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

时间:2022-01-29 15:24:08

i am using google api for ruby, but not know how to start, just give me an ABC example someone, thanks very much?

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

2 个解决方案

#1


11  

If you are creating a Service Account application to access Google Analytics.

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

  1. Register it with Google through https://code.google.com/apis/console. On API Access tab, click Create client ID, choose Service Account. Store the key file Google will generate, and remember the password for that key.
  2. 通过https://code.google.com/apis/console将其注册到Google。在API Access选项卡上,单击Create client ID,选择Service Account。存储Google将生成的密钥文件,并记住该密钥的密码。

  3. Here is some code to get you started

    这里有一些代码可以帮助您入门

    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)
    

Once a key is available, initialize the asserter with your client ID (email in APIs console) and authorization scopes.

一旦密钥可用,请使用您的客户端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

#2


0  

I've answered something similar in a couple of other posts I found that were like this one... so incase its relevant, for ruby, using the google-api-client (for any of the google apis), there's a few ins and outs with authentication when using an api key as opposed to OAuth...

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

I've outlined this process (using an api key server side) at the code abode.

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

You have to explicitly set the authorzation param to nil when constructing the client, otherwise the gem tries to use OAuth to authenticate, so if calling from a server using an api key only, you will always get a 401 Unauthorized.

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

the code abode - google-api-client for ruby

代码住所 - 用于ruby的google-api-client

#1


11  

If you are creating a Service Account application to access Google Analytics.

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

  1. Register it with Google through https://code.google.com/apis/console. On API Access tab, click Create client ID, choose Service Account. Store the key file Google will generate, and remember the password for that key.
  2. 通过https://code.google.com/apis/console将其注册到Google。在API Access选项卡上,单击Create client ID,选择Service Account。存储Google将生成的密钥文件,并记住该密钥的密码。

  3. Here is some code to get you started

    这里有一些代码可以帮助您入门

    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)
    

Once a key is available, initialize the asserter with your client ID (email in APIs console) and authorization scopes.

一旦密钥可用,请使用您的客户端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

#2


0  

I've answered something similar in a couple of other posts I found that were like this one... so incase its relevant, for ruby, using the google-api-client (for any of the google apis), there's a few ins and outs with authentication when using an api key as opposed to OAuth...

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

I've outlined this process (using an api key server side) at the code abode.

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

You have to explicitly set the authorzation param to nil when constructing the client, otherwise the gem tries to use OAuth to authenticate, so if calling from a server using an api key only, you will always get a 401 Unauthorized.

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

the code abode - google-api-client for ruby

代码住所 - 用于ruby的google-api-client