I’m trying to create a simple app to pull in tweets from different timelines and so am taking the Application Only Auth approach as per https://dev.twitter.com/docs/auth/application-only-auth
I’ve created the app and have keys/secrets/tokens coming out of my ears, all is going well. It seems that these are being accepted by https://api.twitter.com/oauth2/token as I’m getting an error back:
[errors] => Array (  => stdClass Object ( [code] => 170 [label] => forbidden_missing_parameter [message] => Missing required parameter: grant_type ) )
I’ve tried sending the variable grant_type in a query and as an array through cURL but it just doesn’t see it and gives me this error every time!
Here is my PHP code:
$url = "https://api.twitter.com/oauth2/token"; $oauth_access_token = "XXXX"; $oauth_access_token_secret = "XXXX"; $consumer_key = "XXXX"; $consumer_secret = "XXXX"; $oauth = array( 'grant_type' => 'client_credentials'); $headercreds = base64_encode($consumer_key.':'.$consumer_secret); $header = "POST /oauth2/token HTTP/1.1\n"; $header .= "Host: api.twitter.com\n"; $header .= "User-Agent: Conscious Clients Feeds\n"; $header .= "Authorization: Basic ".$headercreds."\n"; $header .= "Content-Type: application/x-www-form-urlencoded;charset=UTF-8\n"; $header .= "Content-Length: 29\n"; // Make Requests $feed = curl_init(); $header = array($header); $options = array( CURLOPT_HTTPHEADER => $header, CURLOPT_POSTFIELDS => http_build_query($oauth), CURLOPT_HEADER => 0, CURLOPT_POST => 1, CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => 0 ); curl_setopt_array($feed, $options); $json = curl_exec($feed); curl_close($feed); $twitter_data = json_decode($json);
Can you see any glaring problem with this code? As I said, I’ve tried passing the grant_type var as a query and as an array (removed http_build_query() from $oauth ).
Any help much appreciated!