【发布时间】:2011-05-06 03:12:00
【问题描述】:
我正在尝试构建一些简单的 Ruby 脚本以在本地机器上运行并自动化一些 Twitter 功能,仅供我自己使用。我很难获得 OAuth 授权。
我在 http://dev.twitter.com/apps 注册了应用程序并检索了它的使用者密钥和秘密,我从 http://dev.twitter.com/apps/<application_id>/my_token 检索了我的 Twitter 帐户的访问令牌和访问秘密
将其归结为最少的代码:
require 'oauth'
TWITTER_CONSUMER_KEY = "my application's consumer key"
TWITTER_CONSUMER_SECRET = "my application's consumer secret"
TWITTER_ACCESS_TOKEN = 'my access token'
TWITTER_ACCESS_SECRET = 'my access secret'
consumer = OAuth::Consumer.new(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET,
{ :site => "http://api.twitter.com",
:scheme => :header
})
token_hash = { :oauth_token => TWITTER_ACCESS_TOKEN,
:oauth_token_secret => TWITTER_ACCESS_SECRET
}
access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
response = access_token.request(:get, "http://api.twitter.com/1/friends/ids.json?user_id=Mashable")
puts response.body
每次都给我以下信息。
{"error":"此方法需要身份验证。","request":"\/1\/friends\/ids.json?user_id=Mashable"}
因此,Twitter 似乎没有认识到授权已经发生。我不知道为什么。我错过了什么?
【问题讨论】: