使用 oauth 和 twitter ruby​​ gems 时不断收到 OAuth::Unauthorized 错误 [英] Keep getting OAuth::Unauthorized error when using oauth and twitter ruby gems

查看:42
本文介绍了使用 oauth 和 twitter ruby​​ gems 时不断收到 OAuth::Unauthorized 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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=fmy2aMvnjVgaFrz37bJ4JuB8r5xN79gsgDQRG4BNY which then hits this code:

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'

代码在这一行失败:

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

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

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?

推荐答案

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

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.

这篇关于使用 oauth 和 twitter ruby​​ gems 时不断收到 OAuth::Unauthorized 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆