2016-05-20 85 views
0

这是我第一次使用Twitter API的经验。 我使用以下工具:401未经授权使用Twitter API与Ruby中的oauth gem

  • 红宝石2.2.0p0(2014年12月25日修订版49005)[x86_64的Linux的]
  • 宝石 '的OAuth'

    • 的OAuth(0.5。 1)
    • 的oauth2(1.1.0)
    • omniauth-的oauth2(1.4.0)

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

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

=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 

我试了一下:

  • 得到一个新的密钥和密码。

    结果:

    的Net :: HTTPUnauthorized 401需要授权readbody =真

  • 同步我的服务器的时间,因为许多堆栈溢出帖子提到,则返回401,如果服务器时间的变化超出某一点。我安装了ntp。在Twitter上http://127.0.0.1:3000/auth/twitter/callback

  • API Console Tool:从this list

    建议这里赞赏。

    UPDATEOAuth 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 
    

    预期的数据。

    [{ “created_at”: “周五5月20日11时05分21秒+0000 2016”, “ID”:733614584754515968, “ID_STR”:
    “733614584754515968”, “文”:“三通过 @sitepointdotcom //吨,二氧化碳/ 1p9AxO5JPg”, “截断:技能每个新程序员应该 了解HTTPS”:假的, “实体”:{ “井号标签”:[], “符号” (截)...

  • 回答

    -1

    在这一行上,您应该将“APIKey”和“APISecret”替换为您从CONSUMER_ *环境变量中提取的内容。

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

    来自Twitter的示例代码适合我。错误的用户密钥肯定会给你401。

    +0

    谢谢!对于我来说,过多的这一点是不熟悉的,不知道发生了什么。我感谢帮助! – DMfll

    相关问题