我正在尝试构建一些简单的 Ruby 脚本以在本地计算机上运行并自动执行一些 Twitter 功能,仅供我自己使用。我在获得 OAuth 授权时遇到困难。
我在 http://dev.twitter.com/apps 注册了应用程序并检索了它的消费者 key 和 secret ,我从 http://dev.twitter.com/apps/<application_id>/my_token 检索了我的 Twitter 帐户的访问 token 和访问 secret 。
将其归结为最少的代码:
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":"This method requires authentication.","request":"\/1\/friends\/ids.json?user_id=Mashable"}
所以看起来 Twitter 没有意识到授权已经发生。我不知道为什么。我错过了什么?
最佳答案
您是否在您的请求中使用了验证者密码?大多数 OAuth API 仅要求验证者获取访问 token 。 Twitter 的 API 为每个 API 请求使用验证器。
关于ruby - 无法使用 Oauth 获得简单的单用户 Twitter 自动化 Ruby 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4115120/