为什么Google的自定义搜索API说我在使用Ruby客户端时缺少访问令牌?
我正在尝试通过Google API Ruby客户端使用Google的自定义搜索API 。 我已通过Google API控制台设置了我的API密钥,并且还创建了我的CSE。 根据文档,似乎只要我提供API密钥(我正在做),我就不需要OAuth2身份validation令牌来调用list方法。 但是,当我尝试执行下面的代码时,我收到以下错误:
ArgumentError:缺少访问令牌。
我错过了什么? 这是我的代码:
# create client client = Google::APIClient.new # Fetch discovery doc search = client.discovered_api('custom search') # Call list method response = client.execute( search.cse.list, 'key' => '', 'cx' => '', 'alt' => 'json', 'q' => 'hello world' )
我相信这实际上是客户端的一个错误(它在alpha中)。 在摆弄了一点之后,我找到了一个解决方法:
在创建客户端对象之后,为其分配一个“虚拟”访问令牌:
client.authorization.access_token = '123'
那么你可以调用search.cse.list方法而不会得到’ArgumentError:Missing access token’。 错误。
如果您刚刚使用带有ruby的Google CSE,请尝试使用google-cse 。 我刚刚建造了gem,虽然我私下里已经使用了一段时间。 比alpha客户端更容易使用
我发现在我的代码中添加client.retries = 3
可以解决这个问题。
使用当前版本的gem(0.7.1),除了设置密钥外,还需要将授权设置为nil
:
require 'google/api_client' client = Google::APIClient.new client.key = ENV['GOOGLE_API_KEY'] client.authorization = nil client.execute ...
- Rails中的plusAPmains.circles.addPeople的Google API生成403 Forbidden
- 通过Google Gmail API创建草稿
- JSON解析Google API自定义搜索错误
- 如何使用带有Ruby Google API客户端的Gmail API发送消息?
- cron crawler使用Ruby中的Google API将数据插入Google电子表格的授权问题
- 客户端未经授权使用此方法检索访问令牌
- 使用Ruby中的Google Apps API和服务帐户时出现问题
- 使用Google API Client,如何创建客户端
- 为什么Google API无论输入设置了+01偏移量?