简体   繁体   English

Omniauth-Twitter 401未经授权

[英]Omniauth-Twitter 401 Unauthorized

I'm providing a "log in with twitter" link to /auth/twitter , which the omniauth gem handles. 我正在提供一个“登录twitter”链接到/auth/twitteromniauth gem处理这个omniauth

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). 在我的开发机器上,这似乎工作正常,重定向到twitter登录页面并返回到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 访问生产服务器上的/auth/twitter路径会导致

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 . 我确保服务器确实使用我的开发密钥/秘密,通过在/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. 它们与twitter页面上给出的匹配,并且注册的回调是有效地址并指向生产服务器。

What could possibly be wrong here? 这可能有什么问题? Could this be a result of rate limiting? 这可能是速率限制的结果吗?

Gemfile follows: Gemfile如下:

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: /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. 在我的情况下,问题是服务器时间落后大约50分钟,并且请求被拒绝,因为时间戳太旧了。

Updating the system time fixed it. 更新系统时间修复了它。

don't forget to check the check-box in the twitter settings: 不要忘记检查Twitter设置中的复选框:

"Allow this application to be used to Sign in with Twitter" “允许此应用程序用于使用Twitter登录”

Hope this helps someone 希望这有助于某人

My solution was that you need to have a callback url specified in the Twitter.com Application Management section. 我的解决方案是你需要在Twitter.com应用程序管理部分指定一个回调网址。 https://apps.twitter.com/ This will let localhost urls work as well. https://apps.twitter.com/这将使localhost网址也能正常运行。

My problem was that I had checked "Callback Url locking". 我的问题是我检查了“Callback Url locking”。 Just un-checked it at it worked. 只是取消了它的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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