我正在使用Twitter专用的身份验证并尝试获取搜索结果。我大部分时间都在使用Codeigniter,但有问题的代码只是简单的PHP。
当我在我的localhost上运行它时,它很高兴地返回搜索结果,但在我的服务器上运行它会给我以下错误:
[code] => 99 [label] => authenticity_token_error [message] =>无法验证您的凭据
因此,在第一次请求获取持有人令牌时失败了。
这对我来说有点麻烦,显然很难调试。我可能遗漏了一些非常基本的东西,所以任何帮助都会被感激地接受!
以下是我的代码:
// get consumer key and consumer secret from my config files
$consumer = array(
'key' => $this->config->item($provider.'_key'),
'secret' => $this->config->item($provider.'_secret'),
);
// encode it into the format Twitter expects
$bearerTokenCred = base64_encode($consumer['key'].":".$consumer['secret']);
// set up request
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Authorization: Basic '.$bearerTokenCred."\r\n".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
'content' => 'grant_type=client_credentials'
)
);
$context = stream_context_create($opts);
// send request
$result = file_get_contents('https://api.twitter.com/oauth2/token', false, $context);
$result2 = json_decode($result, true);
// this is where it prints my error - prior to doing anything else
print_r($result2);
if ($result2["token_type"] != "bearer"){
echo $result2["token_type"];
return;
}
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$result2["access_token"]
)
);
$context = stream_context_create($opts);
$result3 = file_get_contents('https://api.twitter.com/1.1/search/tweets.json?q='.urlencode($search), false, $context);
答案 0 :(得分:1)
我实际上并没有按照规定设法解决问题,但在发送的标头(http://www.php.net/manual/en/function.stream-context-create.php#99353)中使用字符串与数组进行了一些追逐 - 这仍然没有做任何事情对我来说。
最后我使用CURL来完成这项工作。
我的假设是它与我的服务器配置有关,但我无法告诉你什么!
下面的最终工作代码
$provider = 'twitter';
// Get keys from the config
$consumer = array(
'key' => $this->config->item($provider.'_key'),
'secret' => $this->config->item($provider.'_secret'),
);
$bearerTokenCred = base64_encode($consumer['key'].":".$consumer['secret']);
$data = array ('grant_type' => 'client_credentials');
$data = http_build_query($data);
$header = array(
"Authorization: Basic $bearerTokenCred",
"Content-type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: " . strlen($data)
);
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => "https://api.twitter.com/oauth2/token",
CURLOPT_RETURNTRANSFER => true
);
$options[CURLOPT_POSTFIELDS] = $data;
$feed = curl_init();
curl_setopt_array($feed, $options);
$result = curl_exec($feed);
curl_close($feed);
$result2 = json_decode($result, true);
if ($result2["token_type"] != "bearer"){
echo "error - invalid token type";
return;
}
$header = array(
'Authorization: Bearer '.$result2["access_token"]
);
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => 'https://api.twitter.com/1.1/search/tweets.json?q='.urlencode('"#'.$search.'"'),
CURLOPT_RETURNTRANSFER => true
);
$feed = curl_init();
curl_setopt_array($feed, $options);
$result = curl_exec($feed);
curl_close($feed);
$result3 = json_decode($result, true);
foreach($result3['statuses'] as $status){
echo "<p>".$status['text']."</p>";
}
我希望这对那里的人有用!