【问题标题】:Rails: OAuth::Unauthorized 401 Authorization Required using OmniAuth-TwitterRails:使用 OmniAuth-Twitter 需要 OAuth::Unauthorized 401 授权
【发布时间】:2014-11-10 21:53:11
【问题描述】:

我已经实现了 StackOverFlow 中的几种不同策略,但到目前为止,似乎没有一个会影响引发的错误:

OAuth::Unauthorized 401 Authorization Required

我正在关注Ryan Bates' RC #241 并点击“使用 Twitter 登录”并收到错误消息。我继续将响应路由添加到routes.rb 文件中,如下所示:

routes.rb:

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

认为错误可能是由回调函数引起的。同样的错误。看看我的dev.log 会发现:

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>'

所以我知道问题在于 Twitter 的身份验证出了问题。一定是KEY和SECRET吧?

现在,根据 Stack 各处的建议,我已将 KEY 和 SECRET 作为 ENV[] 变量、environment/development.rb 文件的直接字符串、取出“ENV[]”变量等。

我的 KEY 和 SECRET 现在位于自定义配置中,如 here... 所讨论的那样

config/initializers/social_media.rb:

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

config/initializers/omniauth.rb 文件:

OmniAuth.config.logger = Rails.logger

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

ActionController: Exception caught OAuth::Unauthorized - 401 Authorization Required 有任何想法吗?这可能是 Noob 错误,但我的 Google-Fu 现在只是 Google-F'ed...

【问题讨论】:

    标签: ruby-on-rails-3 twitter omniauth


    【解决方案1】:

    熬了一夜后,我查看了Twitter developer console上的回调URL。

    为自己省点麻烦,别忘了设置这个。 RailsCast 中没有直接提到它,尽管 Ryan 确实略过它。

    在设置回调 URL 时,不要只输入//localhost:3000 它将不起作用。而是使用:

    http://127.0.0.1:3000/
    

    【讨论】:

    • 或者您可以使用解析为 127.0.0.1 的 lvh.me:3000
    • 如果您使用的是“巫术”gem,则会出现错误 - 请参阅此解决方案:github.com/NoamB/sorcery/issues/540#issuecomment-65752910
    • 这是救命稻草。我正在使用 RailsApp 教程,这部分不是很清楚。
    • 成功了。但是我以后必须将其更改回我的域吗?
    • 在我的情况下指定了回调 URL,但我的源网络应用程序使用 oauth dynamic setup 并需要传递自定义回调 URL。但是在我的推特设置中,我检查了选项Enable Callback Locking(正如推特推荐的那样),我开始收到这个 401 错误。我取消选中选项 Enable Callback Locking 并且身份验证开始工作。
    【解决方案2】:

    在使用 omniauth-twitterdevise gems 处理 Rails 6 应用程序时,我遇到了同样的问题

    我已将 API KeyAPI Secret Key 添加到我的 Rails 6 应用程序中,但是当我尝试测试 Twitter 身份验证时,我遇到了以下错误:

    OAuth::Unauthorized 401 Authorization Required
    

    我是这样解决的

    我将以下回调 URL 添加到我的 Twitter 开发者帐户:

    http://localhost:3000/auth/twitter
    http://localhost:3000/auth/twitter/callback
    http://localhost:3000/users/auth/twitter
    http://localhost:3000/users/auth/twitter/callback
    

    注意:将localhost:3000 替换为您的实际host。此外,用于回调 URL 的 路由 应该与您的应用程序中设置的匹配。

    资源How to Sign in with Twitter using Devise, Omniauth, and Ruby on Rails

    就是这样。

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-15
      • 2012-06-24
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多