【发布时间】:2012-09-16 08:48:15
【问题描述】:
在我的机器上遇到了一些SSL issues 之后,我仍在尝试通过 Google Ruby Client API 访问用户的 Blogger 帐户。我正在使用以下内容:
- Rails 3.2.3
- Ruby 1.9.3
- oauth2 (0.8.0)
- omniauth (1.1.1)
- omniauth-google-oauth2 (0.1.13)
- google-api-client (0.4.6)
我可以在认证时通过 Google API 成功认证用户并访问他们的博客。当用户登录时,我存储从 Google 收到的 access_token 和 refresh_token。在access_token 到期之前一切正常。我正在尝试构建将refresh_token 交换为新access_token 的功能,但不断遇到墙壁。以client documentation 为例,这是我正在使用的代码:
client = Google::APIClient.new
token_pair = auth.oauth_token # access_token and refresh_token received during authentication
# Load the access token if it's available
if token_pair
client.authorization.update_token!(token_pair.to_hash)
end
# Update access token if expired
if client.authorization.refresh_token && client.authorization.expired?
client.authorization.fetch_access_token!
end
blogger = client.discovered_api('blogger', 'v3')
result = client.execute(
api_method: blogger.blogs.list_by_user,
parameters: {'userId' => "self", 'fields' => 'items(description,id,name,url)'},
headers: {'Content-Type' => 'application/json'})
当access_token 有效时,此代码可以完美运行。不过,一旦到期,我就会看到 2 个问题:
- 即使我知道令牌已过期(我已经检查了数据库中的
expires_at值),client.authorization.expired?返回false-- 除了使用数据库中的值? - 当我强制执行
client.authorization.fetch_access_token!时,我收到invalid_request错误。
谁能告诉我如何使用客户端 API 将 refresh_token 换成新的 access_token?即使你知道如何用另一种语言来做,那也会有很大的帮助,因为我可以尝试 Rubyfy 它。谢谢!!
【问题讨论】:
标签: ruby-on-rails oauth-2.0 omniauth access-token google-api-client