简体   繁体   中英

401 Unauthorized with omniauth-twitter

I'm trying to use omniauth-twitter in a rails app, but i'm stuck at the very beginning and can't figure out what the cause is. I did register my application in dev.twitter.com and I got the CONSUMER_KEY and CONSUMER_SECRET. To shorten test time I put these two variables directly in initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, "an_alphanumeric_code", "some_other_alphanumeric_code"
end

In application.html.erb layout I have the link to Twitter Sign in

<%= link_to "Sign in with Twitter", "auth/twitter" %>

But when I click to sign in, instead of being redirected to twitter login I get

401 Unauthorized

I already checked other questions on SO and this should not be a computer time problem (my rails server has time set correctly). I suspect that the problem lies in not having a public IP and server name available for testing.

Is there someone who has been able to have omniauth-twitter working from a local 127.0.0.1:3000 rails server?

Did you put the Callback URL in the application while registering the app? If the Callback URL is missing then this is the first problem you will encounter.

Omniauth Twitter works fine for local rails dev environments. Things to check:

Your route is set up correctly for the callback, something like:

match "/auth/:provider/callback" => "sessions#create"

Also notice the potentially missing leading slash in the path in your link_to. Try changing to:

<%= link_to "Sign in with Twitter", "/auth/twitter" %>

In my implementation of Omniauth Twitter (which ought to be the same as others) it doesn't take you to a login or confirmation screen if you are already signed into Twitter. It redirects you back to the url you specify in dev.twitter.com. So check your app to see if it is progressing through to Twitter for authentication (use Firebug or Chrome dev tools). That should help track down your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM