google-api-client gem,fetch_access_token! - “非法请求”

时间:2013-02-28 08:32:18

标签: oauth-2.0 google-calendar-api google-oauth google-api-client google-api-ruby-client

以下是我要做的事情 -

client = Google::APIClient.new
client.authorization.client_id = 'XXXX'
client.authorization.client_secret = 'XXXXX'
client.authorization.scope = 'https://www.googleapis.com/auth/calendar'
client.authorization.redirect_uri = 'http://www.aaaaa.com/'
calendar = client.discovered_api('calendar', 'v3')

为了获得access_token进一步请求,我打电话给 client.authorization.fetch_access_token!

我得到的回应是:

Signet::AuthorizationError: Authorization failed.  Server message:
{
  "error" : "invalid_request"
}
from D:/main/tools/jruby/lib/ruby/gems/1.8/gems/signet-0.4.4/lib/signet/oauth_2/client.rb:865:in `fetch_access_token'

后来我更改了一些更改并将grant_type设置为password并提供了用户名和密码。

client.authorization.grant_type = 'password'
client.authorization.username = 'aaaaa'
client.authorization.password = 'aaaaa'

仍面临同样的问题。

文档没有多大帮助。我有什么设置吗?

1 个答案:

答案 0 :(得分:0)

你可能已经想到了这一点,但我想我知道问题是什么。您必须先(在浏览器中)导航到client.authorization.authorization_uri,这将要求您授予对Google帐户的访问权限。之后,您将被重定向到client.authorization.redirect_uri,即您提供的代码中的http://www.aaaaa.com/。将在URL上附加授权码:http://www.aaaaa.com?code=<code here>。您必须将client.authorization.code设置为等于该授权码。完成后,您应该可以调用client.authorization.fetch_access_token!,然后调用API。