我正在为 Twitter 使用仅限应用程序的身份验证并尝试获取搜索结果。我大部分时间都在使用 Codeigniter,但有问题的代码只是普通的 PHP。
当我在本地主机上运行它时,它会愉快地返回搜索结果,但是在我的服务器上运行它会给我以下错误:
[代码] => 99 [标签] =>authentity_token_error [消息] => 无法验证您的凭据
所以它在第一次请求获取我的不记名令牌时失败了。
这对我来说似乎有点古怪,显然很难调试。我可能缺少一些非常基本的东西,所以任何帮助都会被感激地接受!
下面是我的代码:
// 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);