我正在使用google-api-client客户端gem for ruby并在我调用API时收到403 Access Not Configured错误。
require 'google/api_client'
client = Google::APIClient.new
client.authorization = nil
search = client.discovered_api('customsearch')
response = client.execute( search.cse.list, 'key' => '<<MY KEY>>', 'cx' => '<<MY CX>>', 'alt' => 'json', 'q' => 'hello world')
我正在尝试不使用OAuth进行搜索,只使用API密钥。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:1)
我遇到了同样的问题,我解决了这个问题:
Google帐户设置:
Ruby on Rails代码:
# creating client instance
client = Google::APIClient.new
# authenticating
key = Google::APIClient::PKCS12.load_key("#{Rails.root}/config/<STRANGE_LONG_FILENAME>.p12", 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => '<SCOPE_URL>',
:issuer => '<HASH>@developer.gserviceaccount.com',
:signing_key => key)
client.authorization.fetch_access_token!
# API call
# NOTE: Check the documentation for API methods and parameters). The method discovered_api returns a service object. We can use to_h.keys to get the list of available keys of that object. Keys represents API methods (e.g. "analytics.management.accounts.list" the API method path is "management.accounts.list").
result = client.execute(
:api_method => client.discovered_api('<SERVICE_NAME>', 'v3').management.accounts.list,
:parameters => { accountId: '~all', webPropertyId: '~all'}
)
if result.success?
result.data
end
答案 1 :(得分:0)
未配置访问意味着它是一个API,要求您的项目在API控制台中注册,并从服务选项卡打开自定义搜索API。您的客户端由OAuth令牌或API密钥标识。必须提供一个或两个。我怀疑您刚刚在服务选项卡中打开API,因为您的示例包含API密钥参数。
您也可以尝试更明确地传递参数:
result = client.execute(
:api_method => search.cse.list,
:key => '<<MY KEY>>',
:parameters => {
'cx' => '<<MY CX>>',
'alt' => 'json',
'q' => 'hello world'
}
)