问题描述
我想简单地发送给谷歌的托管模式sample.sentiment的请求。我无法弄清楚如何通过谷歌使用OAuth 2.0认证,而且我得到无尽的金额小时。如果你能提供给我的code到这将是有益的。以下是我工作过的。
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' => ''})
`
推荐答案
好了,在网络上的例子可能会有点混乱,但如果你想利用一台服务器 - 服务器通信 - 这意味着没有最终用户也没有浏览器是参与,那么下面code应该为你工作:
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:
在prerequisites:
The prerequisites:
- 您应该产生在你的谷歌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 => prediction.hostedmodels。predict
或这样的事情。我没有用托管的模型呢。
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
调用返回的可能是有趣的字段是:
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.
这篇关于我如何使用OAuth 2.0授权使用Ruby谷歌的predictive API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!