2011-04-13 73 views
4

我在無意識的脫髮點我試圖刷新紅寶石雅虎OAuth訪問令牌。幫助提神雅虎的OAuth訪問令牌在Ruby中

使用OmniAuth和OAuth的寶石,我能夠得到雅虎的訪問令牌,但它在一個小時後到期。

我正在關注的雅虎指令refresh an expired token,和我始終返回401

如果有人能告訴我如何刷新訪問使用OAuth的寶石的道理,我會大大讚賞。

+1

您還可以在WWW-驗證響應 - 從雅虎發送的標題,以查看有關401響應返回原因的更多信息。它會說類似「oauth_problem = timestamp_refused」或「oauth_problem = parameter_absent&oauth_parameters_absent = oauth_timestamp」。 – Crashthatch 2011-07-01 11:01:07

回答

4

首先,請確保您從原來的get_access_token調用保存oauth_session_handle參數。

然後,當你正在尋找刷新的access_token做這樣的事情:

request_token = OAuth::RequestToken.new(consumer, 
             config["ACCESS_TOKEN"],    
             config["ACCESS_TOKEN_SECRET"]) 
token = OAuth::Token.new(config["ACCESS_TOKEN"], 
         config["ACCESS_TOKEN_SECRET"]) 
@access_token = request_token.get_access_token(
         :oauth_session_handle => config["SESSION_HANDLE"], 
         :token => token) 

...其中...

config["ACCESS_TOKEN"] is your old access token 
config["ACCESS_TOKEN_SECRET"] is your old secret 
config["SESSION_HANDLE"] is your oauth_session_handle 
consumer is your OAuth::Consumer.new reference 

我存儲在一個YAML文件中的配置變量和然後在啓動時加載它。

記住的@access_token存儲下一次。

我在YDN OAuth Forum適應這種從一個答案。

+0

完美的作品,謝謝! – Chris 2011-09-30 13:35:22

+0

出色地工作! – Sid 2012-10-28 20:50:04

+0

SESSION_HANDLE是必要的。在我的代碼中,我錯過了它,所以也得到「token_rejected」 – Stony 2014-08-11 08:44:45

1

注:oauth_session_handle返回爲一個PARAM通過調用get_access_token:

access_token   = request_token.get_access_token(:oauth_verifier => oauth_verifier) 
oauth_session_handle = access_token.params['oauth_session_handle'] 

這是不到明顯從看oauth-ruby/oauth代碼