为什么这个twitter oauth API令牌请求失败

时间:2012-08-16 18:38:53

标签: oauth

[注意:以下所有oauth令牌/秘密都是随机创建的;他们是 不是我的实际代币/秘密]

 
curl -o /tmp/test.txt 'https://api.twitter.com/oauth/request_token?
oauth_timestamp=1345141469&
consumer_key=UEIUyoBjBRomdvrVcUTn&oauth_access_token_secret=YePiEkSDFdYAOgscijMCazcSfBflykjsEyaaVbuJeO&oauth_access_token=47849378%2drZlzmwutYqGypbLsQUoZUsGdDkVVRkjkOkSfikNZC&oauth_nonce=1345141469&
consumer_secret=rUOeZMYraAapKmXqYpxNLTOuGNmAQbGFqUEpPRlW&
oauth_version=1%2e0&
oauth_signature_method=HMAC%2dSHA1&oauth_signature=H0KLLecZNAAz%2bXoyrPRiUs37X3Zz%2bAcabMa5M4oDLkM' 

[为了清晰起见我添加了新行;实际命令是一行]

假设所有其他数据都有效,为什么上面的命令会产生 “无法验证oauth签名和令牌”(即使我使用我的 真实数据)?

特别是我的签名 “H0KLLecZNAAz%2bXoyrPRiUs37X3Zz%2bAcabMa5M4oDLkM”无效,或者我是 做一些更根本的错误。

我用来生成这个程序:


#!/bin/perl 
use Digest::SHA; 

%twitter_auth_hash = ( 
"oauth_access_token" => "47849378-rZlzmwutYqGypbLsQUoZUsGdDkVVRkjkOkSfikNZC", 
"oauth_access_token_secret" => "YePiEkSDFdYAOgscijMCazcSfBflykjsEyaaVbuJeO", 
"consumer_key" => "UEIUyoBjBRomdvrVcUTn", 
"consumer_secret" => "rUOeZMYraAapKmXqYpxNLTOuGNmAQbGFqUEpPRlW" 
); 

# if uncommented, pull my actual data 
# require "bc-private.pl"; 

$twitter_auth_hash{"oauth_signature_method"} = "HMAC-SHA1"; 
$twitter_auth_hash{"oauth_version"} = "1.0"; 
$twitter_auth_hash{"oauth_timestamp"} = time(); 
$twitter_auth_hash{"oauth_nonce"} = time(); 

for $i (keys %twitter_auth_hash) { 
  push(@str,"$i=".urlencode($twitter_auth_hash{$i})); 
} 

$str = join("&",@str); 

# thing to sign 
$url = "GET $str"; 

# signing it 
$sig = urlencode(Digest::SHA::hmac_sha256_base64($url, "rUOeZMYraAapKmXqYpxNLTOuGNmAQbGFqUEpPRlW&YePiEkSDFdYAOgscijMCazcSfBflykjsEyaaVbuJeO")); 

# full URL incl sig 
$furl = "https://api.twitter.com/oauth/request_token?$str&oauth_signature=$sig"; 
# system("curl -o /tmp/testing.txt '$furl'"); 


print "FURL: $furl\n"; 
print "STR: $str\n"; 
print "SIG: $sig\n"; 

sub urlencode { 
  my($str) = @_; 
  $str=~s/([^a-zA-Z0-9])/"%".unpack("H2",$1)/iseg; 
  $str=~s/ /\+/isg; 
  return $str; 
} 

注意:我意识到还有许多其他可能的原因导致失败, 但目前的问题是:我是否正确发送参数而且是 我正确地计算了签名。

1 个答案:

答案 0 :(得分:1)

Twitter要求您执行POST for the request token