在我的计算机上与SSL issues挣扎后,我仍然试图通过Google Ruby客户端API访问用户的Blogger帐户。我使用了以下内容:Rails Google客户端API - 无法为访问令牌交换刷新令牌
- 的Rails 3.2.3
- 的Ruby 1.9.3
- 的oauth2(0.8.0)
- omniauth(1.1.1)
- omniauth - 谷歌 - 的oauth2 (0.1.13)
- 谷歌的API客户端(0.4.6)
我能成功验证的用户,并通过谷歌访问一个自己的博客PI在认证时。当用户登录时,我会存储我从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
是有效的。只要它虽然到期,我看到两个问题:
- 尽管我知道令牌已过期(我已经在数据库中检查
expires_at
值),client.authorization.expired?
回报false
- 有一种不同的方式除了使用数据库中的值之外,我可以检查令牌的到期日期吗? - 当我强制执行
client.authorization.fetch_access_token!
时,出现invalid_request
错误。
有人可以让我知道如何使用客户端API将refresh_token
换成access_token
?即使你知道如何用另一种语言来做,这将是一个很大的帮助,因为我可以尝试Rubyfy它。谢谢!!
brimil01,你是男人!我甚至不知道为什么会这样,我的其他尝试进行HTTP调用没有,但我一直在看这个12个小时,所以我明天就会明白。你的代码完全可以工作,你已经从大规模的头撞中拯救了我,非常感谢。 – cerrina
谢谢。一直试图弄清楚这一点。 –
我发布这个时似乎得到了这个: #“invalid_request”},@response =# ,@headers = {“cache-control”=> [“no-cache,no-store,max-age = 0,must-revalidate”],“pragma”=> [“no-cache”],“expires” =“[”Fri,01 Jan 01 00:00:00 GMT“],”date“=> [”Wed,27 Mar 2013 08:50:48 GMT“],”content-type“=> [”application/json“],”x-content-type-options“=> [”nosniff“],”x-frame-options“=> [”SAMEORIGIN“],”x-xss-protection“=> [”1; mode ='block'],“server”=> [“GSE”],“connection”=> [“close”]}> ... –