简体   繁体   中英

Omniauth-Twitter 401 Unauthorized

I'm providing a "log in with twitter" link to /auth/twitter , which the omniauth gem handles.

On my development machine, this appears to work fine, redirecting to the twitter login page and returning to the callback set for the application at dev.twitter.com (http://127.0.0.1:3000/auth/twitter/callback).

I have a separate application registered for our test production server, with the only things different being the access token, secret, and callback. Accessing the /auth/twitter path on the production server results in

OAuth::Unauthorized (401 Unauthorized):
  oauth (0.4.6) lib/oauth/consumer.rb:216:in `token_request'
  oauth (0.4.6) lib/oauth/consumer.rb:136:in `get_request_token'
  ...

I've made sure that the server is indeed using my development key/secret by printing the ones used to the log as they are being set in /config/initializers/omniauth.rb . They match the ones given on the twitter page, and the callback registered is a valid address and points to the production server.

What could possibly be wrong here? Could this be a result of rate limiting?

Gemfile follows:

source 'https://rubygems.org'

gem 'rails', '3.2.3'

gem 'mysql2','0.3.11'

gem 'tweetstream'
gem 'koala'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'twitter-bootstrap-rails'
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

gem 'twitter'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'thin'

/config/initializers/omniauth.rb:

puts "initializing twitter with #{TWITTER_KEY}, #{TWITTER_SECRET}"
Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, TWITTER_KEY, TWITTER_SECRET
  provider :facebook, FACEBOOK_KEY, FACEBOOK_SECRET,
        :scope => 'read_stream,publish_stream'
end

The server logs the correct key and secret on the first line here.

In my case, the problem was that the server time was about 50 minutes behind, and the requests were being rejected because the timestamp was too old.

Updating the system time fixed it.

don't forget to check the check-box in the twitter settings:

"Allow this application to be used to Sign in with Twitter"

Hope this helps someone

My solution was that you need to have a callback url specified in the Twitter.com Application Management section. https://apps.twitter.com/ This will let localhost urls work as well.

My problem was that I had checked "Callback Url locking". Just un-checked it at it worked.

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