Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: OAuth::Unauthorized 401 Authorization Required using OmniAuth-Twitter

I have implemented several different strategies found in StackOverFlow, but so far, none seem to affect the error being thrown:

OAuth::Unauthorized 401 Authorization Required

I am following Ryan Bates' RC #241 and get to the point where I click "Sign-in with Twitter" and I get the error. I went ahead and added the response route to the routes.rb file as listed here:

routes.rb:

match 'auth/twitter/callback', to: 'user#update'

thinking that the error might be caused from the callback function. Same error. A look at my dev.log shows this:

Started GET "/auth/twitter" for 127.0.0.1 at 2014-09-16 18:52:08 -0600
(twitter) Request phase initiated.

OAuth::Unauthorized (401 Authorization Required):
oauth (0.4.7) lib/oauth/consumer.rb:216:in `token_request'
oauth (0.4.7) lib/oauth/consumer.rb:136:in `get_request_token'
omniauth-oauth (1.0.1) lib/omniauth/strategies/oauth.rb:29:in `request_phase'
omniauth-twitter (1.0.1) lib/omniauth/strategies/twitter.rb:60:in `request_phase'
omniauth (1.2.2) lib/omniauth/strategy.rb:215:in `request_call'
omniauth (1.2.2) lib/omniauth/strategy.rb:183:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
...
script/rails:6:in `require'
script/rails:6:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'

So I know the issue is with the authentication with Twitter going out. Must be the KEY and SECRET, right?

Now, I have put the KEY and SECRET in as ENV[] variables, as direct strings to the environment/development.rb file, taken out the "ENV[]" variables, etc., as per suggestions found all over Stack.

My KEY and SECRET now reside in a custom configuration as discussed here...

config/initializers/social_media.rb:

TWITTER_CONFIG = YAML.load_file("#{::Rails.root}/config/twitter.yml")[::Rails.env]

The config/initializers/omniauth.rb file:

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, TWITTER_CONFIG['app_id'], TWITTER_CONFIG['secret']
end   

Any ideas on the ActionController: Exception caught OAuth::Unauthorized - 401 Authorization Required? This is probably a Noob error, but my Google-Fu is just Google-F'ed right now...

like image 685
Matteo Avatar asked Sep 17 '14 01:09

Matteo


People also ask

What is OAuth 2 and omniauth?

OAuth 2 and OmniAuth OAuth 2 is an authorization protocol that enables a third-party applications to obtain limited access to an HTTP service. One of the main aspects of this protocol is the access token that is issued to the application. This token is used by the app to perform various actions on the user’s behalf.

Does the from_omniauth () method resolve authentication errors?

This indeed rescues any error that was raised inside the from_omniauth method. However, this does not protect us from the errors that happened during the authentication. For example, if you disable cookies and try to authenticate via one of the social networks, you’ll get a SessionExpired error.

Where can I find a working demo of OAuth2?

A working demo can be found at sitepoint-oauth2.herokuapp.com. OAuth 2 is an authorization protocol that enables a third-party applications to obtain limited access to an HTTP service. One of the main aspects of this protocol is the access token that is issued to the application.


1 Answers

After a night of tearing my hair out, I took at look at the callback URL on Twitter developer console.

Save yourselves some trouble and don't forget to set this. It's not mentioned directly in the RailsCast, although Ryan does briefly pass over it.

When you set the callback URL, don't just put //localhost:3000 it won't work. Instead use:

http://127.0.0.1:3000/
like image 121
Matteo Avatar answered Oct 05 '22 23:10

Matteo