如何使用 OAuth 2.0 为 Ruby 中的谷歌预测 API 授权?

问题描述:

我试图简单地向谷歌的托管模型 sample.sentiment 发送请求.我无法弄清楚如何通过 google 使用 Oauth 2.0 进行授权,而且我的工作时间很长.如果你能向我提供代码,这将是有帮助的.这是我的工作.

I am trying to simply send a request to google's hosted model sample.sentiment. I cannot figure out how to authorize with Oauth 2.0 via google, and I am getting endless amounts of hours. If you could provide me the code to this is would be helpful. Here is what I am working off of.

client = Google::APIClient.new({:application_name => "CCE",:application_version => "1.0"} )
plus = client.discovered_api('prediction')

# Initialize OAuth 2.0 client    
client.authorization.client_id = 'my client id'
client.authorization.client_secret = 'my client secret'
client.authorization.redirect_uri = 'my callback url'

client.authorization.scope = 'https://www.googleapis.com/auth/prediction'

# Request authorization
redirect_uri = client.authorization.authorization_uri

# Wait for authorization code then exchange for token
client.authorization.code = '....'
client.authorization.fetch_access_token!

# Make an API call
 result = client.execute(
   :api_method => plus.activities.list,
   :parameters => {'hostedModelName' => 'sample.sentiment', 'userId' => ''})

`

嗯,网上的例子可能有点混乱,但如果你想利用服务器-服务器通信 - 这意味着没有最终用户和没有涉及浏览器,那么以下代码应该适合您:

Well, the examples on the web may be a little confusing, but if you want to utilize a server-server communication - meaning that no end-user and no browser is involved, then the following code should work for you:

先决条件:

  • 您应该在 Google API 控制台中生成一个服务帐户"密钥.它会提示您下载私钥,您应该将其存储在磁盘上的某个位置,例如 client.p12(或使用原始名称,但为了清楚起见,我将使用较短的名称).
  • you should generate a "service account" key in your Google API Console. It will prompt you to download a private key, which you should store somewhere on your disk, for example as client.p12 (or use the original name, but for clarity I'll use the shorter one).
client = Google::APIClient.new(
  :application_name => "CCE",
  :application_version => "1.0"
)
prediction = client.discovered_api('prediction', 'v1.5')

key = Google::APIClient::KeyUtils.load_from_pkcs12('client.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 => 'https://www.googleapis.com/auth/prediction',
  :issuer => '..put here your developer email address from Google API Console..',
  :signing_key => key,
)
client.authorization.fetch_access_token!

# Now you can make the API calls
result = client.execute(...

值得注意的是,client.discovered_api 调用似乎需要版本号.否则它可能会抛出异常NotFound".

Worth noting is that client.discovered_api call seems to require the version number. otherwise it may be throwing an exception 'NotFound'.

密码实际上是字符串notasecret"!

The passphrase is really the string 'notasecret'!

另一件事:在 API 调用中确保调用正确的方法 - 对于托管模型,我相信您可以调用的唯一方法是 :api_method =>predict.hostedmodels.predict 或类似的东西.我还没有使用托管模型.(有关详细信息,请参阅 API 文档)

Another thing: in API call make sure that you call the proper method - for hosted models I believe the only method you can call is :api_method => prediction.hostedmodels.predict or something like this. I've not used hosted models yet. (see the API docs for details)

client.execute 调用返回的 result 可能有趣的字段是:

The possibly interesting fields of the result returned by the client.execute call are:

result.status
result.data['error']['errors'].map{|e| e['message']} # if they exist
JSON.parse(result.body)

如果您检查它们,它们可能会极大地帮助您调试任何问题.

If you inspect them, they will probably help you significantly in debugging any problems.