OAuth::Unauthorized 401 int twitter-omniauth gem

robert picture robert · Dec 6, 2011 · Viewed 9.6k times · Source

I have been trying to authenticate users using twitter-omniauth gem for last days, yet not successful. (Authentication with facebook works perfectly)

I'm keep getting 401 Unauthorized error.

I search through stackoverflow, but none of the answers could solve my problem.

I reach the twitter login when I try http://127.0.0.1/users/auth/twitter. I login and I'm redirected to http://127.0.0.1/users/auth/twitter/callback and unauthorized error comes.

Below callback url I have entered in twitter

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

rake routes output

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"}

If you need anymore info, I'll provide. Please help me to solve this.

Answer

Kalvir Sandhu picture Kalvir Sandhu · Dec 13, 2011

I had this issue today and found that I had to set a callback URL in the settings at dev.twitter.com under my application. So check that you have set a callback URL settings, I set it to my intended production address and when running in dev mode locally it redirected fine.

It mentions under the callback url input box that if left blank it will restrict the app from using callback urls.