php - Twitter app-only auth works on localhost, but not on server -
i'm using app-only auth twitter , trying search results. i'm using codeigniter part, code in question plain php.
it happily returns search results when i'm running on localhost, running on server gives me following error:
[code] => 99 [label] => authenticity_token_error [message] => unable verify credentials
so it's failing @ first request bearer token.
this seems bit screwy me , hard debug. i'm missing quite basic, gratefully accepted!
below code:
// consumer key , consumer secret config files $consumer = array( 'key' => $this->config->item($provider.'_key'), 'secret' => $this->config->item($provider.'_secret'), ); // encode format twitter expects $bearertokencred = base64_encode($consumer['key'].":".$consumer['secret']); // set 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); // prints error - prior doing 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 managed solve problem stated, went on few goose-chases concerning use of strings vs arrays in sent header (http://www.php.net/manual/en/function.stream-context-create.php#99353) - still didn't me.
in end resorted using curl did job.
my assumption it's server configuration, couldn't tell what!
final working code below
$provider = 'twitter'; // keys 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'] $status){ echo "<p>".$status['text']."</p>"; }
i hope of use out there!
Comments
Post a Comment