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);