繁体   English   中英

如何授权使用google-api-ruby-client

[英]how to authorizate use google-api-ruby-client

我正在使用谷歌api的红宝石,但不知道如何开始,只是给我一个ABC示例的人,非常感谢?

如果您要创建服务帐户应用程序以访问Google Analytics。

  1. 通过https://code.google.com/apis/console将其注册到Google。 在API Access选项卡上,单击Create client ID,选择Service Account。 存储Google将生成的密钥文件,并记住该密钥的密码。
  2. 这里有一些代码可以帮助您入门

     require 'rubygems' require 'google/api_client' api_client = Google::APIClient.new path_to_key_file ="/path/to/key/file-privatekey.p12" passphrase = "google_generated_password" key = Google::APIClient::PKCS12.load_key(path_to_key_file, passphrase) 

密钥可用后,使用您的客户端ID(API控制台中的电子邮件)和授权范围初始化断言器。

asserter = Google::APIClient::JWTAsserter.new(
   'super_long_client_id_from_api_console@developer.gserviceaccount.com',
   'https://www.googleapis.com/auth/analytics.readonly',
   key)

# To request an access token, call authorize:
api_client.authorization = asserter.authorize()
puts api_client.authorization.access_token

http://code.google.com/p/google-api-ruby-client/wiki/ServiceAccounts

我在其他一些帖子中回答了类似的问题,我发现它们就像这一个...因此,对于ruby,使用google-api-client(对于任何谷歌api),有一些ins使用api密钥而不是OAuth时使用身份验证进行...

我在代码居住区概述了这个过程(使用api密钥服务器端)。

在构造客户端时,您必须将授权参数显式设置为nil,否则gem会尝试使用OAuth进行身份验证,因此如果仅使用api密钥从服务器调用,则始终会获得401 Unauthorized。

代码abode - 用于ruby的google-api-client

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM