使用 oauth 和 twitter ruby​​ gems 时不断收到 OAuth::Unauthorized 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1280295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:37:57  来源:igfitidea点击:

Keep getting OAuth::Unauthorized error when using oauth and twitter ruby gems

ruby-on-railsrubytwitteroauth

提问by Tony

I am using the ruby twitter gem and oauth to gain access to users twitter accounts. In my code, I have:

我正在使用 ruby​​ twitter gem 和 oauth 来访问用户的 twitter 帐户。在我的代码中,我有:

unless @user.twitter_authd?
      oauth = Twitter::OAuth.new('token', 'secret')
      session[:twitter_request_token] = oauth.request_token.token
      session[:twitter_request_secret] = oauth.request_token.secret
      @twitter_auth_url = oauth.request_token.authorize_url
    end

where token and secret have my actual token and secret inserted. When I click on the link to the @twitter_auth_url, I am taken to twitter and asked to grant access. I click allow and then twitter redirects me to my callback URL http://www.mydomain.com/twitter_callback/?oauth_token=fmy2aMvnjVgaFrz37bJ4JuB8r5xN79gsgDQRG4BNYwhich then hits this code:

其中令牌和秘密插入了我的实际令牌和秘密。当我点击@twitter_auth_url 的链接时,我被带到 Twitter 并被要求授予访问权限。我点击允许,然后 twitter 将我重定向到我的回调 URL http://www.mydomain.com/twitter_callback/?oauth_token=fmy2aMvnjVgaFrz37bJ4JuB8r5xN79gsgDQRG4BNY然后点击以下代码:

oauth = Twitter::OAuth.new('token', 'secret')

    logger.info("session[:twitter_request_token] = #{session[:twitter_request_token]}")
    logger.info("session[:twitter_request_secret] = #{session[:twitter_request_secret]}")

    oauth.authorize_from_request(session[:twitter_request_token], session[:twitter_request_secret])
    session[:twitter_request_token] = nil
    session[:twitter_request_secret] = nil

    @user.update_attributes({
      :twitter_token => oauth.access_token.token, 
      :twitter_secret => oauth.access_token.secret,
    })

    redirect_to root_path

The twitter request token and secret are being set just fine. However I end up with an authorization error:

推特请求令牌和秘密设置得很好。但是我最终遇到了授权错误:

 OAuth::Unauthorized in MainController#twitter_callback

401 Unauthorized

RAILS_ROOT: /Users/TAmoyal/Desktop/RoR_Projects/mls
Application Trace | Framework Trace | Full Trace

/Library/Ruby/Gems/1.8/gems/oauth-0.3.4/lib/oauth/consumer.rb:167:in `token_request'
/Library/Ruby/Gems/1.8/gems/oauth-0.3.4/lib/oauth/tokens/request_token.rb:14:in `get_access_token'
/Library/Ruby/Gems/1.8/gems/erwaller-twitter-0.6.13.1/lib/twitter/oauth.rb:29:in `authorize_from_request'
/Users/TAmoyal/Desktop/RoR_Projects/mls/app/controllers/main_controller.rb:70:in `twitter_callback'

The code is failing at this line:

代码在这一行失败:

oauth.authorize_from_request(session[:twitter_request_token], session[:twitter_request_secret])

when it tries to get an access token. You can see the source code of authorize_from_request here. I am not sure why this is happening. Anyone have ideas?

当它尝试获取访问令牌时。您可以在此处查看 authorize_from_request 的源代码。我不确定为什么会这样。有人有想法吗?

采纳答案by Tony

This was one of the most annoying things to debug that I have come across. I was outputting in a couple places by accident because the URL's are dynamic and they happened to not be defined in my test case (i use this to display chart data and there is not enough right now so the google chart api URL's are blank). This caused my browser to make multiple requests to my localhost when some pages were loaded. Somehow that made the oauth process crap out. Obviously there is no way for people on S.O. to know about my application specific issue so I had to answer my own question.

这是我遇到的最烦人的调试事情之一。我意外地在几个地方输出,因为 URL 是动态的,而且它们碰巧没有在我的测试用例中定义(我用它来显示图表数据,现在没有足够的数据,所以谷歌图表 api URL 是空白的)。这导致我的浏览器在加载某些页面时向我的本地主机发出多个请求。不知何故,这使 oauth 过程变得糟糕。显然,SO 上的人无法了解我的应用程序特定问题,因此我必须回答我自己的问题。

回答by George Palmer

A bit late to the party but just ran into the same issue myself. I tracked the issue down to the setup of my OAuth app in Twitter. I had initially not specified a callback URL as I was unsure of it.

参加聚会有点晚,但我自己也遇到了同样的问题。我将问题追溯到在 Twitter 中设置我的 OAuth 应用程序。我最初没有指定回调 URL,因为我不确定。

Once I had setup my rails app I went back to find Twitter had assumed I was a desktop application as I hadn't specified a callback URL. Once I changed this to website and entered a callback URL I stopped getting 400s.

一旦我设置了我的 rails 应用程序,我回去发现 Twitter 假设我是一个桌面应用程序,因为我没有指定回调 URL。一旦我将其更改为网站并输入回调 URL,我就不再收到 400。

回答by Hatem Mahmoud

If you're getting error 401 - OAuth::Unauthorized, make sure you edit the settings of your Twitter application as follows:

如果您收到错误 401 - OAuth::Unauthorized,请确保按如下方式编辑 Twitter 应用程序的设置:

回答by Rabia

this is an issue about time synchronization of your system with twitter server.

这是关于您的系统与 twitter 服务器的时间同步的问题。

回答by Thank you

Twitter doesn't allow localhostas part of a valid callback URL.

Twitter 不允许localhost作为有效回调 URL 的一部分。

Instead use http://127.0.0.1:3000/auth/twitter/callback

而是使用 http://127.0.0.1:3000/auth/twitter/callback

Hope this helps

希望这可以帮助

回答by Kenny Wyland

I had this same problem and none of the suggestions in this thread worked for me.

我遇到了同样的问题,该线程中的所有建议都不适合我。

I found the problem for me was the TIMESTAMP on my request. The mobile device I was running my scripts on had a Hymaned up clock. When I updated the system time on my device to the correct time (i.e. now), all of my requests came back "200 OK" instead of "401 Unauthorized".

我发现我的问题是我请求的时间戳。我运行脚本的移动设备有一个时钟。当我将设备上的系统时间更新为正确的时间(即现在)时,我的所有请求都返回“200 OK”而不是“401 Unauthorized”。

回答by vadim

This problem seems to be caused by twitter not being able to handle connection keep-alivecorrectly. Make sure you set connection=close http headerin the request to twitter. Wasted a weekend debugging this.

这个问题似乎是由 twitter 无法connection keep-alive正确处理引起的。确保您connection=close http header在对 twitter 的请求中进行了设置。浪费了一个周末调试这个。

回答by pjammer

not enough info for me, but when was twitter gem last updated? twitter changed their oauth 'stuff' in mid may approx. perhaps you have an old one. I'd update your question to show the callback_url, and make sure you have the right token and secret, which it looks like you don't have.

对我来说没有足够的信息,但是 twitter gem 上次更新是什么时候?推特大约在 5 月中旬更改了他们的 oauth '东西'。也许你有一个旧的。我会更新您的问题以显示 callback_url,并确保您拥有正确的令牌和秘密,而您似乎没有。

also, did you put the right callback url in your twitter app page? alot of times that screws you up too.

另外,您是否在 Twitter 应用程序页面中放置了正确的回调 URL?很多时候你也搞砸了。

if that fails use mbleighs twitter_authinstead. it worked for me and is pretty slick.

如果失败,请改用 mbleighs twitter_auth。它对我有用,而且非常光滑。