Twitter API php oauth身份验证失败

时间:2015-04-07 17:46:06

标签: php twitter twitter-oauth

如果我的twitterInput值是用户名,则身份验证正常。但如果它是一个标签,它就会失败。搜索查询也正确编码。

我的是?q =%23hashtagName,不是?q =%2523hashtagName。我意识到我之前编码太多了。

正在正确生成所有身份验证代码。我已经测试了$ base_string,$ key,$ signature,$ url,$ auth和$ options以查看它们是否保持正确的值。据我所知,它们看起来很好。

我收到了代码32的消息,“无法对您进行身份验证”。

我试图看看我的所有变量之间是否存在使用标签或用户名的差异,除了明显的路径差异外,我无法发现主要差异。

有人能发现导致主题标签失败的明显问题吗?

谢谢。

这是我的整个功能:

<?php
function get_tweets($twitterInput){  
$token = 'XXXXXXXX';
$token_secret = 'XXXXXXXX';
$consumer_key = 'XXXXXXXX';
$consumer_secret = 'XXXXXXXX';

$host = 'api.twitter.com';
$method = 'GET';

$hashtagInput = null;
$usernameInput = null;
$twitterFirstCharacter = $twitterInput[0];

// detect whether the input was a hashtag, username with the @ symbol, or just username without @
if ($twitterFirstCharacter == "#") {
    $hashtagInput = $twitterInput;
} else if ($twitterFirstCharacter == "@") {
    // just in case there's a @ symbol, delete it
    $usernameInput = substr($twitterInput, 1);
} else {
    $usernameInput = $twitterInput;
}

$path = null;

// if hashtag, build a different path 
if ($hashtagInput) {
    $path = '/1.1/search/tweets.json'; // api call path
    $query = array( // query parameters
        "q" => $hashtagInput, 
        "count" => 3,
    );
} else {
     $path = '/1.1/statuses/user_timeline.json'; // api call path
     $query = array( // query parameters
        'screen_name' => $usernameInput,
        'count' => 3
    );
}

$oauth = array(
    'oauth_consumer_key' => $consumer_key,
    'oauth_signature_method' => 'HMAC-SHA1',
    'oauth_timestamp' => time(),
    'oauth_nonce' => time() . '-' . (string)mt_rand(), // a stronger nonce is recommended
    'oauth_version' => '1.0',
    'oauth_token' => $token,
  );

$arr = array_merge($oauth, $query); // combine the values THEN sort

asort($arr); // secondary sort (value)
ksort($arr); // primary sort (key)

$querystring = urldecode(http_build_query($arr, '', '&'));

$url = "https://$host$path";

$base_string = $method."&".urlencode($url)."&".urlencode($querystring);

  // same with the key
$key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);

  // generate the hash
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));

$url = $url . "?" . http_build_query($query);

$oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
ksort($oauth); // probably not necessary, but twitter's demo does it

  // also not necessary, but twitter's demo does this too
$oauth = array_map("_twitter_add_quotes", $oauth);

  // this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));

  // if you're doing post, you need to skip the GET building above
  // and instead supply query parameters to CURLOPT_POSTFIELDS
$options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                  //CURLOPT_POSTFIELDS => $postfields,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

// do our business
$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

return $twitter_data;
}
function _twitter_add_quotes($str) { return '"'.$str.'"'; }
?>

1 个答案:

答案 0 :(得分:0)

最好使用Twitter API库,看一下Twitter Developers页面,它有一套很好的PHP和其他语言库。