在我的計算機上與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」]}> ... –