2011-12-06 67 views
15

我一直在嘗試使用twitter-omniauth gem來驗證用戶的最後日子,但沒有成功。 (與Facebook的身份驗證完美)OAuth :: Unauthorized 401 int twitter-omniauth gem

我一直得到401未經授權的錯誤。

我通過stackoverflow搜索,但沒有答案可以解決我的問題。

當我嘗試http://127.0.0.1/users/auth/twitter時,我到達twitter登錄。我登錄後,我被重定向到http://127.0.0.1/users/auth/twitter/callback,併發生未經授權的錯誤。

下面回調URL我已經在Twitter上

http://127.0.0.1/users/auth/twitter/callback

rake routes輸出

new_user_session GET /users/sign_in(.:format)    {:action=>"new", :controller=>"devise/sessions"} 
       user_session POST /users/sign_in(.:format)    {:action=>"create", :controller=>"devise/sessions"} 
     destroy_user_session DELETE /users/sign_out(.:format)    {:action=>"destroy", :controller=>"devise/sessions"} 
     user_omniauth_callback  /users/auth/:action/callback(.:format) {:action=>/twitter|facebook/, :controller=>"users/omniauth_callbacks"} 
       user_password POST /users/password(.:format)    {:action=>"create", :controller=>"devise/passwords"} 
      new_user_password GET /users/password/new(.:format)   {:action=>"new", :controller=>"devise/passwords"} 
      edit_user_password GET /users/password/edit(.:format)   {:action=>"edit", :controller=>"devise/passwords"} 
          PUT /users/password(.:format)    {:action=>"update", :controller=>"devise/passwords"} 
    cancel_user_registration GET /users/cancel(.:format)    {:action=>"cancel", :controller=>"devise/registrations"} 
      user_registration POST /users(.:format)      {:action=>"create", :controller=>"devise/registrations"} 
     new_user_registration GET /users/sign_up(.:format)    {:action=>"new", :controller=>"devise/registrations"} 
     edit_user_registration GET /users/edit(.:format)     {:action=>"edit", :controller=>"devise/registrations"} 
          PUT /users(.:format)      {:action=>"update", :controller=>"devise/registrations"} 
          DELETE /users(.:format)      {:action=>"destroy", :controller=>"devise/registrations"} 
         login  /login(.:format)      {:action=>"login", :controller=>"home"} 
         root  /         {:controller=>"home", :action=>"index"} 

如果你需要再進入的信息,我會提供。請幫我解決這個問題。

+0

羅伯特 - 你有沒有在你的初始化程序中檢查你的應用程序ID和應用程序密碼是否正確?它們是否與Twitter顯示的本地應用程序相匹配? – TallGreenTree

+0

另外,你可以發佈你的用戶/ omniauth_callbacks控制器的代碼嗎? – TallGreenTree

+0

@TallGreenTree我有雙倍檢​​查appication id和應用程序的祕密很多次..它是相同的... – robert

回答

13

我敢肯定,我遇到了問題,這個時候它是來自本機的...

This article說,他們允許,但如果沒記錯的話,我不得不使用一個外部訪問的URL。當我測試我的oauth東西時,我使用DynDns並在我的路由器中打開了一個端口。

+5

如果您使用的是本地主機。它適用於如果您使用URL縮短器。 – Benjamin

+0

是的 - 我用ngrok,它的工作就像一個魅力。謝謝ejlevin1 –

17

我今天有這個問題,發現我必須在我的應用程序下的dev.twitter.com設置中設置一個回調URL。因此,請檢查您是否設置了回撥URL設置,我將其設置爲預期的製作地址,並在本地以開發模式運行時重定向。

它在回調url輸入框中提到,如果留空,它將限制應用程序使用回調URL。

+1

我在問題中提到過,我已經給出了回調url ... – robert

+1

我有這個相同的問題,並提供回調url修復它。我認爲你需要讓域名與應用註冊的域名相同。所以對於網站:我有http://www.example.com和回調網址:http://www.example.com/auth/twitter/callback因此,Kalvir說不要使用http://127.0.0.1作爲基本網址,當您註冊您的應用程序時,請使用您在網站字段中輸入的內容。 – wuliwong

2

在我的情況下,我使用「訪問令牌」和祕密,而不是「消費者密鑰」導致此錯誤。

+0

當使用twitter-omniauth時,您應該使用訪問令牌,而不是「使用者密鑰」 – wspruijt

1

在我的情況下,我們使用的是Timecop gem,它導致omniauth-twitter gem失敗,因爲與本地服務器時間的衝突由timecop設置。

1

我遇到了這個問題,因爲我把我的密鑰放在我的環境中〜/ .bash_profile,但我從來沒有找到它。一旦我打開一個新的終端,它工作得很好。

1

顯然,Twitter在「應用程序設置」選項卡上添加了一個複選框。

在「應用程序詳細信息」窗格的底部,您必須選中「允許此應用程序用於使用Twitter登錄」複選框。

經過檢查並等待幾分鐘後,警報框告訴我一切都開始工作。