本文介绍了谷歌API客户端的Rails的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用红宝石谷歌的API客户端的客户端宝石和得到一个403访问未配置错误,每当我调用API。
I'm using the google-api-client client gem for ruby and getting a 403 Access Not Configured error whenever I call the API.
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密钥。
I'm trying to search without using OAuth, and just the API key.
任何帮助将是AP preciated。谢谢!
Any help would be appreciated. Thank you!
推荐答案
我有同样的问题,我解决了它是这样的:
I had the same problem and I solved it like this:
谷歌帐户设置:
- 转到谷歌蜜蜂()。
- 在服务启用该服务,您想使用(例如Analytics(分析)API)。
- 在API访问通过点击蓝色的大按钮,创建一个OAuth客户端ID。
- 在下一屏幕上输入您的产品信息(名称,标识,URL)。
- 在下一屏幕上,因为通信会由服务器处理,我们检查服务帐户选项,并单击创建客户端ID按钮。
- 下载刚生成的私钥,并把它({} Rails.root /config/.p12)。
- 将打开API访问页面,因为您需要在下一步的一些参数值。
- 在新标签,转到并选择您想使用该服务。在左侧的菜单导航到配置>管理API(第3版)>资源>授权(的).在下表中,你会找到合适的:范围参数的URL(如),这是你如何找到适当的范围。
- 注意:发行人参数是的旁边显示的客户端ID(如@ developer.gserviceaccount.com)的服务帐户的电子邮件地址
- 对于检查URL(如会youtubeAnalytics,V1)。我usualy发现谷歌搜索的例子。)
- 请注意,是公钥指纹的价值。
- 请检查源$ C $ c。在返回。
- 也许你将不得不发行人的电子邮件地址添加到您的谷歌Analytics帐户。
- Go to Google Apis (https://code.google.com/apis/console/).
- Under "Services" enable the service that you would like to use (e.g. Analytics API).
- Under "API Access" create a OAuth Client ID by clicking on the big blue button.
- On the next screen enter your product information (name, logo, url).
- On the next screen, because the communication will handled by the server, we check the "Service Account" option and click the Create Client ID button.
- Download the private key that was just generated and put it to ({Rails.root}/config/.p12).
- Leave the API Access page opened because you will need some parameter values in the next step.
- In new tab, go to https://developers.google.com/products/ and choose the service that you would like to use. In the left menu navigate to Configuration > Management API (v3) > Resources > Authorization (https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization). In the table below you will find the appropriate ":scope" parameter url (e.g. https://www.googleapis.com/auth/analytics.readonly). This is how you find the appropriate scope.
- Note that the ":issuer" parameter is the email address of the service account that's displayed next to the Client ID (e.g. @developer.gserviceaccount.com).
- For the checking the URL (e.g. https://developers.google.com/youtube/analytics/v1/ would be youtubeAnalytics, v1). I usualy find that googling examples :).
- Note that the is the "Public key fingerprints" value.
- Do check the source code at https://github.com/google/google-api-ruby-client for each object returned.
- Maybe you will have to add the issuer email address to your Google Analytics Account.
Ruby on Rails的code:
# 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
这篇关于谷歌API客户端的Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!