2012-07-17 59 views
0

我看到了許多關於同一問題的問題,但我無法找到解決方法,所以我在問一個問題以確保我沒有錯過任何東西。無法使用oauth_verifier獲取訪問令牌,不斷收到401錯誤

在twitter上,我將callback_url設置爲'https:// my_app_url /'。

# consumer 
@consumer = OAuth::Consumer.new('consumer_id', 
           'secret_key', 
           :site => 'https://api.twitter.com') 

# request_token 
@request_token = @consumer.get_request_token(:oauth_callback => "https://my_app_url/?state=#{state}") 

# then get the user to log in via 
login_url = @request_token.authorize_url 

# I have a params[:oauth_token] and params[:oauth_verifier] in return 
# I don't know what the oauth token is for 
# I then should be able to get an access token 

# all oauth calls are done in an object so @request_token has been conserved. 
@access_token = @request_token.get_access_token(:oauth_verifier => params[:oauth_verifier]) 

# then it happens. 
# => 401 Unauthorized 

我不知道爲什麼401被提出。有沒有一個我已經失蹤的步驟? 我將不勝感激任何幫助。

回答

1

get_request_token爲您提供臨時OAuth令牌,以便在OAuth身份驗證流程中對您進行身份驗證。 OAuth驗證程序證明用戶授權應用程序。您必須將此最後一項提供給get_access_token才能獲得將用於所有已驗證請求的最終OAuth令牌。

如需進一步信息,請考慮閱讀官方Twitter文檔處理這個:https://dev.twitter.com/docs/auth/using-oauthhttps://dev.twitter.com/docs/auth/implementing-sign-twitter對過程的描述)

+0

感謝您的解釋。我在我的代碼中遺漏了一個錯誤,並且request_token沒有正確保存,所以我從一個新生成的request_token中獲得了access_token,這與我獲得的oauth_verifier不匹配。 – oldergod 2012-07-18 00:19:43