谷歌API客户端的Rails [英] Google API Client Rails

查看:145
本文介绍了谷歌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:

谷歌帐户设置:


  1. 转到谷歌蜜蜂(的https://$c$c.google.com/的API /控制台/ )。

  2. 在服务启用该服务,您想使用(例如Analytics(分析)API)。

  3. 在API访问通过点击蓝色的大按钮,创建一个OAuth客户端ID。

  4. 在下一屏幕上输入您的产品信息(名称,标识,URL)。

  5. 在下一屏幕上,因为通信会由服务器处理,我们检查服务帐户选项,并单击创建客户端ID按钮。

  6. 下载刚生成的私钥,并把它({} Rails.root /config/.p12)。

  7. 将打开API访问页面,因为您需要在下一步的一些参数值。

  8. 在新标签,转到 https://developers.google.com/products/ 并选择您想使用该服务。在左侧的菜单导航到配置>管理API(第3版)>资源>授权(的https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtAuthorization).在下表中,你会找到合适的:范围参数的URL(如的https:// WWW。 googleapis.com/auth/analytics.readonly ),这是你如何找到适当的范围。

  9. 注意:发行人参数是的旁边显示的客户端ID(如@ developer.gserviceaccount.com)的服务帐户的电子邮件地址

  10. 对于检查URL(如 https://developers.google.com/youtube/分析/ V1 / 会youtubeAnalytics,V1)。我usualy发现谷歌搜索的例子。)

  11. 请注意,是公钥指纹的价值。

  12. 请检查源$ C ​​$ c。在 https://github.com/google每个对象/谷歌的API-红宝石客户返回。

  13. 也许你将不得不发行人的电子邮件地址添加到您的谷歌Analytics帐户。

  1. Go to Google Apis (https://code.google.com/apis/console/).
  2. Under "Services" enable the service that you would like to use (e.g. Analytics API).
  3. Under "API Access" create a OAuth Client ID by clicking on the big blue button.
  4. On the next screen enter your product information (name, logo, url).
  5. 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.
  6. Download the private key that was just generated and put it to ({Rails.root}/config/.p12).
  7. Leave the API Access page opened because you will need some parameter values in the next step.
  8. 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.
  9. 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).
  10. For the checking the URL (e.g. https://developers.google.com/youtube/analytics/v1/ would be youtubeAnalytics, v1). I usualy find that googling examples :).
  11. Note that the is the "Public key fingerprints" value.
  12. Do check the source code at https://github.com/google/google-api-ruby-client for each object returned.
  13. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆