我正在使用 Google API Ruby 客户端来访问用户的日历。
我可以通过以下方式访问:
client_id: "xxxxx"
client_secret: "xxxxx"
access_type: "offline"
approval_type: ""
scope: "https://www.google.com/calendar/feeds/ https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar"
callback_path: "/app/oauth_response"
provider :google_oauth2, GOOGLE_API['client_id'], GOOGLE_API['client_secret'],
{ access_type: GOOGLE_API['access_type'],
approval_prompt: GOOGLE_API['approval_prompt'],
scope: GOOGLE_API['scope'],
callback_path: GOOGLE_API['callback_path'],
path_prefix: GOOGLE_API['path_prefix']}
当响应返回时,它包含刷新 token 、访问 token 、expired_at 等。然后我就可以使用访问代码发出 API 请求。但是一旦该访问代码过期(一小时后),我相信我需要使用刷新 token 来获取新的访问 token ,对吗?
这是我正在调用的电话:
HTTParty.get('https://www.google.com/calendar/feeds/default/owncalendars/full', :query => {:alt => "jsonc", :access_token => access_token})
这告诉我我的 token 已过期。所以我尝试买一件新的。 但当我尝试这样做时,我得到了这个:
@client.authorization.fetch_access_token!
ArgumentError Exception: Missing authorization code.
我在我的@client对象中看到@code=nil。我假设这就是需要设置的内容,但我没有从初始请求中返回“代码”属性。
我如何获得该代码,或者如果我不需要它,我做错了什么? 谢谢!
最佳答案
很可能在您调用 fetch_access_token 时,@client.authorization 中未设置刷新 token !
看看https://github.com/google/signet/blob/master/lib/signet/oauth_2/client.rb
该错误消息仅在未知/未指定的 grant_type 失败时出现。 grant_type 本身是根据授权客户端的状态推断出来的。
在您的用例中,尝试刷新 token ,它应该是“refresh_token”,并且如果设置了refresh_token,则 grant_type 将返回该值。我的预感是,如果您转储 @client.authorization.refresh_token 和 @client.authorization.grant_type 的值,它们都将为零。
修复方法是确保在调用该方法之前正确恢复刷新 token 。
关于ruby-on-rails - 获取访问 token !在 Google API 中—— "Missing authorization code.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10079149/