简体   繁体   English

401未经omniauth-twitter授权

[英]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. 我正在尝试在Rails应用程序中使用omniauth-twitter,但我一开始就被困住了,无法弄清原因是什么。 I did register my application in dev.twitter.com and I got the CONSUMER_KEY and CONSUMER_SECRET. 我确实在dev.twitter.com中注册了我的应用程序,并且得到了CONSUMER_KEY和CONSUMER_SECRET。 To shorten test time I put these two variables directly in initializers/omniauth.rb 为了缩短测试时间,我将这两个变量直接放在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 在application.html.erb布局中,我具有指向Twitter的链接登录

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

But when I click to sign in, instead of being redirected to twitter login I get 但是当我单击登录时,而不是重定向到Twitter登录,我得到了

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). 我已经检查了SO上的其他问题,这应该不是计算机时间问题(我的Rails服务器的时间设置正确)。 I suspect that the problem lies in not having a public IP and server name available for testing. 我怀疑问题出在没有公共IP和服务器名称可用于测试。

Is there someone who has been able to have omniauth-twitter working from a local 127.0.0.1:3000 rails server? 是否有人能够从本地127.0.0.1:3000 Rails服务器工作omniauth-twitter?

Did you put the Callback URL in the application while registering the app? 注册应用程序时,您是否在应用程序中添加了Callback URL If the Callback URL is missing then this is the first problem you will encounter. 如果缺少Callback URL ,则这是您将遇到的第一个问题。

Omniauth Twitter works fine for local rails dev environments. Omniauth Twitter在本地Rails开发环境中运行良好。 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. 另请注意,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. 在我的Omniauth Twitter(应该与其他版本相同)的实现中,如果您已经登录Twitter,则不会带您进入登录或确认屏幕。 It redirects you back to the url you specify in dev.twitter.com. 它将您重定向回在dev.twitter.com中指定的URL。 So check your app to see if it is progressing through to Twitter for authentication (use Firebug or Chrome dev tools). 因此,请检查您的应用程序,看看它是否正在进入Twitter进行身份验证(使用Firebug或Chrome开发者工具)。 That should help track down your issue. 那应该有助于找出您的问题。

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

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