15

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.

robert
  • 8,459
  • 9
  • 45
  • 70
  • Robert - have you checked your application id and application secret are correct in your initializer? Do they match what Twitter is showing for your local application? – Will Barrett Dec 10 '11 at 16:54
  • Also, could you post the code you have for the users/omniauth_callbacks controller? – Will Barrett Dec 10 '11 at 16:57
  • @TallGreenTree i have double checked appication id and application secret many times.. it is same.... – robert Dec 14 '11 at 15:43
  • I was having this exact same problem (Facebook was working but Twitter wasn't) and this SO question helped me solve it: http://stackoverflow.com/questions/8263675/omniauth-oauthexception-oauthunauthorized – Ashitaka Feb 08 '12 at 21:15

6 Answers6

17

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.

  • 1
    i had this same issue and providing the callback url fixed it. I think that you need to have the domain be the same as the domain that the app is registered for. So for Website: i have http://www.example.com and for callback url: http://www.example.com/auth/twitter/callback So Kalvir is saying don't use http://127.0.0.1 as the base url, use whatever you put in the Website field when you registered your app. – wuliwong Apr 26 '12 at 01:32
13

I'm pretty sure I ran into issues with this when it was coming from localhost...

This article says they allow it, but if memory serves me correctly, I had to use an externally accessible URL. When I was testing my oauth stuff, I used DynDns and opened a port in my router.

ejlevin1
  • 735
  • 5
  • 15
2

In my case I used "Access Token" and secret instead of "Consumer Key" caused this error.

vvsatya
  • 21
  • 2
1

In my case, we were using the Timecop gem and it was causing the omniauth-twitter gem to fail because of a conflict with the local server time being set by timecop.

Billy Coover
  • 3,827
  • 5
  • 36
  • 50
1

I ran into this issue because I had put my keys in my environment in ~/.bash_profile, but i never sourced it. Once i opened a new terminal it worked fine.

msfeldstein
  • 1,080
  • 1
  • 9
  • 18
1

Apparently Twitter added a checkbox on the App Settings tab.

At the bottom of the "Application details" pane, you have to check the box saying "Allow this application to be used to Sign in with Twitter".

After checking it and waiting a few minutes as the alert box is telling me it all began to work.

Jeremy F.
  • 1,346
  • 12
  • 30