使用Ruby中的oauth gem从Twitter API中未经授权使用401

时间:2016-05-20 10:52:13

标签: ruby oauth twitter-oauth

这是我第一次使用Twitter API的经历。 我使用以下工具:

  • ruby​​ 2.2.0p0(2014-12-25修订版49005)[x86_64-linux]
  • gem' oauth'

    • oauth(0.5.1)
    • oauth2(1.1.0)
    • omniauth-oauth2(1.4.0)

我从Twitter获得了密钥和秘密。

我从example on Twitter复制并粘贴了Ruby。

=begin
code taken directly from the example at 
https://dev.twitter.com/oauth/overview/single-user
=end

require 'oauth'
consumer_key, \
  consumer_secret = [
    'CONSUMER_KEY', 
    'CONSUMER_SECRET'
].map { |key| ENV[key] }
raise "Some key undefined." unless [consumer_key, consumer_secret].all?

# Exchange your oauth_token and oauth_token_secret for an AccessToken instance.
def prepare_access_token(oauth_token, oauth_token_secret)
    consumer = OAuth::Consumer.new("APIKey", "APISecret", { :site => "https://api.twitter.com", :scheme => :header })

    # now create the access token object from passed values
    token_hash = { :oauth_token => oauth_token, :oauth_token_secret => oauth_token_secret }
    access_token = OAuth::AccessToken.from_hash(consumer, token_hash )

    return access_token
end

# Exchange our oauth_token and oauth_token secret for the AccessToken instance.
access_token = prepare_access_token(consumer_key, consumer_secret)
p access_token

# use the access token as an agent to get the home timeline
response = access_token.request(:get, "https://api.twitter.com/1.1/statuses/home_timeline.json")
p response

=begin   

|| #<OAuth::AccessToken:0x000000021ed938 
@token="redacted", @secret="redacted", 
@consumer=#<OAuth::Consumer:0x000000021edb68 
@key="APIKey", 
@secret="APISecret", @options={:signature_method=>"HMAC-SHA1", 
:request_token_path=>"/oauth/request_token", 
:authorize_path=>"/oauth/authorize", 
:access_token_path=>"/oauth/access_token", 
:proxy=>nil, :scheme=>:header, 
:http_method=>:post, :oauth_version=>"1.0", 
:site=>"https://api.twitter.com"}>, 
@params={:oauth_token=>"redacted", :oauth_token_secret=>"redacted"}>
|| #<Net::HTTPUnauthorized 401 Authorization Required readbody=true>
=end

我尝试了什么:

建议从哪里开始欣赏。

UPDATE OAuth Tool on Twitter Developer返回卷曲执行时的预期结果:

curl --get 'https://api.twitter.com/1.1/statuses/home_timeline.json' --header 'Authorization: OAuth oauth_consumer_key="redacted", oauth_nonce="redacted", oauth_signature="redacted", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1463742270", oauth_token="redacted", oauth_version="1.0"' --verbose

会返回预期的数据。

  

[{&#34; created_at&#34;:&#34; Fri May 20 11:05:21 +0000   2016&#34;&#34; ID&#34;:733614584754515968&#34; ID_STR&#34 ;:
  &#34; 733614584754515968&#34;,&#34; text&#34;:&#34;每个新程序员应该具备的三项技能   学习https:// t。 co / 1p9AxO5JPg via   @ sitepointdotcom&#34;&#34;截断&#34;:假,&#34;实体&#34; {&#34;#标签&#34;:[],&#34;符号&#34;   (截短的)...

1 个答案:

答案 0 :(得分:-1)

在这一行,您应该替换&#34; APIKey&#34;和#34; APISecret&#34;你从CONSUMER_ *环境变量中提取的内容。

consumer = OAuth::Consumer.new("APIKey", "APISecret", { :site => "https://api.twitter.com", :scheme => :header })

来自Twitter的示例代码对我来说很好。错误的消费者密钥肯定会给你401。