【问题标题】:Twitter API: Constantly getting 401 status using Oauth and ruby gem (Timestamp Out of bounds)Twitter API:使用 Oauth 和 ruby​​ gem 不断获得 401 状态(时间戳越界)
【发布时间】:2013-06-14 01:35:12
【问题描述】:

我是 ruby​​ 的新手,正在尝试使用 Twitters API。我从 twitter 开发网站获得了我的密钥,并将他们的权限设置为“读取、写入和访问直接消息”。然后我尝试使用在 codecademy 上找到的一些代码来检索状态代码,但得到 401 错误(用 X 替换键)。奇怪的是codecademy中的这段代码检索了200个状态码,所以我的想法是我电脑里的东西:

require 'rubygems'
require 'oauth'

    consumer_key = OAuth::Consumer.new(
        "X",
        "X")
    access_token = OAuth::Token.new(
        "X",
        "X")

    # All requests will be sent to this server.
    baseurl = "https://api.twitter.com"

    # The verify credentials endpoint returns a 200 status if
    # the request is signed correctly.
    address = URI("#{baseurl}/1.1/account/verify_credentials.json")

    # Set up Net::HTTP to use SSL, which is required by Twitter.
    http = Net::HTTP.new address.host, address.port
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER

我也尝试使用 twitters gem(当然添加了所需的密钥),这次我收到以下错误 Timestamp out of bounds (Twitter::Error::Unauthorized)

require 'rubygems'
require 'twitter'

Twitter.configure do |config|
  config.consumer_key = YOUR_CONSUMER_KEY
  config.consumer_secret = YOUR_CONSUMER_SECRET
  config.oauth_token = YOUR_OAUTH_TOKEN
  config.oauth_token_secret = YOUR_OAUTH_TOKEN_SECRET
end


Twitter.update("I'm tweeting with @gem!")

我正在运行 ruby​​ 1.9.3p429。

任何帮助都将不胜感激。

【问题讨论】:

标签: ruby twitter oauth gem


【解决方案1】:

我遇到了同样的问题,查看 oauth 文档我发现以下内容:

Make a regular GET request using AccessToken:
@response = @token.get('/people')
@response = @token.get('/people', { 'Accept'=>'application/xml' })

如果您有兴趣,可以在这里找到更多详细信息:http://oauth.rubyforge.org/rdoc/classes/OAuth/AccessToken.html

这里是我的代码:

def prepare_access_token()

  @opts = {
    'oauth_token' => 'TOKEN',
    'oauth_token_secret' => 'TOKEN_SECRET',
    'api_key' => 'API_KEY',
    'api_secret' => 'API_SECRET'
  }

  consumer = OAuth::Consumer.new("#{@opts['api_key']}", "#{@opts['api_secret']}",
                               { :site => "http://api.twitter.com",
                                 :scheme => :header
                               })
  # now create the access token object from passed values
  token_hash = { :oauth_token => "#{@opts['oauth_token']}",
               :oauth_token_secret => "#{@opts['oauth_token_secret']}"
  }
  access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
  return access_token

end

#here is your url
call_url = "https://api.twitter.com/1.1/statuses/home_timeline.json"
uri = URI.encode(call_url)
# Exchange our oauth_token and oauth_token secret for the AccessToken instance.
access_token = prepare_access_token()
# use the access token as an agent to get the home timeline
response = access_token.get(uri)

这对我有用。如果您有反馈,请随时分享:)

【讨论】:

    【解决方案2】:

    从几天前开始,我昨天遇到了同样的问题。发生的事情是我在海外生活了一段时间,并且我将我的地区设置为 X 地区,但手动将我的时钟更改为我现在的时钟。一旦我设置了当前区域并与互联网时区同步,错误就消失了。

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题 (401) 我通过在 twitter 中进入应用程序并重新生成消费者密钥和消费者秘密来解决它,这很容易,因为有一个按钮可以做到这一点,使用新值停止了我的 401错误。希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 2010-11-19
        • 2018-09-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        • 2010-11-01
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多