Twitter API调用不起作用

时间:2013-12-13 12:22:44

标签: twitter twitter-oauth twitter-streaming-api twitter-search

嗨朋友我正在尝试运行twitter apis以使用下面的代码获取主题标签的推文。当我尝试获取用户时间线时,它没有给出任何身份验证错误,但是当它试图搜索包含hahstag的推文时,它会给出身份验证错误。

$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/statuses/user_timeline.json'; // api call path

$query = array( // query parameters
'screen_name' => 'twitterapi'
);

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

$oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
$query = array_map("rawurlencode", $query);

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

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

// http_build_query automatically encodes, but our parameters
// are already encoded, and must be by this point, so we undo
// the encoding step
$querystring = urldecode(http_build_query($arr, '', '&'));

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

// mash everything together for the text to hash
$base_string = $method."&".rawurlencode($url)."&".rawurlencode($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)));

// this time we're using a normal GET query, and we're only encoding the query params
// (without the oauth params)
$url .= "?".http_build_query($query);
$url=str_replace("&","&",$url); //Patch by @Frewuill

$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
function add_quotes($str) { return '"'.$str.'"'; }
$oauth = array_map("add_quotes", $oauth);

// this is the full value of the Authorization line
$auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
echo $auth;exit;
// 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);
echo "<pre>";print_r($twitter_data);

When i run this code i am successfully able to get the user time line so i wnt for next step to get tweets for a particular hashtag by chnaging code like below

$host = 'api.twitter.com';
$method = 'GET';
$path = '/1.1/search/tweets.json'; // api call path

$query = array( // query parameters
'q' => '#Polls2013'
);

但是现在它给出了一个奇怪的错误,如下所示。

stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => Could not authenticate you
[code] => 32
)

)

)

1 个答案:

答案 0 :(得分:0)

您要为搜索发布的查询应按照twitter指定的方式进行网址编码, 请参阅此文档(https://dev.twitter.com/docs/auth/percent-encoding-parameters