I'm using the app-only auth for Twitter and trying to get search results. I'm using Codeigniter for the most part, but the code in question is just plain PHP.
It happily returns search results when I'm running it on my localhost, but running it on my server gives me the following error:
[code] => 99 [label] => authenticity_token_error [message] => Unable to verify your credentials
So it's failing at the first request to get my bearer token.
This seems a bit screwy to me and is obviously hard to debug. I'm probably missing something quite basic, so any help would be gratefully accepted!
Below is my code:
// 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."
".
"Content-type: application/x-www-form-urlencoded;charset=UTF-8
",
'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);
I haven't actually managed to solve the problem as stated, but went on a few goose-chases concerning the use of strings vs arrays in the sent header (http://www.php.net/manual/en/function.stream-context-create.php#99353) - which still didn't do anything for me.
In the end I resorted to using CURL which did the job.
My assumption is that it's something to do with my server configuration, but I couldn't tell you what!
Final working code below
$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>";
}
I hope this is of use to someone out there!