Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter REST API – 'Could not authenticate you' error when using 'tweet_mode=extended' parameter

I'm having a slight problem using the Twitter REST API and cURL when it comes to retrieving data from https://api.twitter.com/1.1/statuses/user_timeline.json

I have the following set up below which works perfectly but as soon as I try to get extended tweets (tweet_mode=extended) but replacing the URLs with https://api.twitter.com/1.1/statuses/user_timeline.json?tweet_mode=extended it errors out and says Could not authenticate you. and if I remove the parameter ?tweet_mode=extended it again works fine.

var $oauthConsumerKey = "XXX"; // Consumer key
var $oauthConsumerKeySecret = "XXX"; // Consumer key secret
var $oauthToken = "XXX"; // Token
var $oauthTokenSecret = "XXX"; // Token secret

$oauthHash = '';
$oauthHash .= 'oauth_consumer_key=' . $this->oauthConsumerKey . '&';
$oauthHash .= 'oauth_nonce=' . time() . '&';
$oauthHash .= 'oauth_signature_method=HMAC-SHA1&';
$oauthHash .= 'oauth_timestamp=' . time() . '&';
$oauthHash .= 'oauth_token=' . $this->oauthToken . '&';
$oauthHash .= 'oauth_version=1.0';

$base = '';
$base .= 'GET';
$base .= '&';
$base .= rawurlencode('https://api.twitter.com/1.1/statuses/user_timeline.json');
$base .= '&';
$base .= rawurlencode($oauthHash);

$key = '';
$key .= rawurlencode($this->oauthConsumerKeySecret);
$key .= '&';
$key .= rawurlencode($this->oauthTokenSecret);

$signature = base64_encode(hash_hmac('sha1', $base, $key, true));
$signature = rawurlencode($signature);

$oauthHeader = '';
$oauthHeader .= 'oauth_consumer_key="' . $this->oauthConsumerKey . '", ';
$oauthHeader .= 'oauth_nonce="' . time() . '", ';
$oauthHeader .= 'oauth_signature="' . $signature . '", ';
$oauthHeader .= 'oauth_signature_method="HMAC-SHA1", ';
$oauthHeader .= 'oauth_timestamp="' . time() . '", ';
$oauthHeader .= 'oauth_token="' . $this->oauthToken . '", ';
$oauthHeader .= 'oauth_version="1.0", ';
$curlHeader = array("Authorization: Oauth {$oauthHeader}", 'Expect:');


$curlRequest = curl_init();
curl_setopt($curlRequest, CURLOPT_HTTPHEADER, $curlHeader);
curl_setopt($curlRequest, CURLOPT_HEADER, false);
curl_setopt($curlRequest, CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json');
curl_setopt($curlRequest, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlRequest, CURLOPT_SSL_VERIFYPEER, false);
$this->twiiterJSON = curl_exec($curlRequest);
curl_close($curlRequest);

I set it up in Postman to check and within Postman it works fine so I generated a PHP cURL code snippet and tested it with that and it also seemed to work fine so can anyone help me figure out what I'm doing wrong? I think it's perhaps to do with the signature but it seemed to work okay without the parameter.

Any help/advice would be appreciated.

like image 387
John the Painter Avatar asked May 02 '18 21:05

John the Painter


1 Answers

Instead of figuring out whats wrong I would just use what works for now

<?php

$username   = REX_VALUE[1];
$key        = REX_VALUE[2];     // Consumer Key (API Key)
$secret     = REX_VALUE[3];     // Consumer Secret (API Secret)
$counter    = '0';              // Zähler, wieviele Tweets ausgegeben werden sollen

setlocale(LC_TIME, "de_DE.utf8");

$quest = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . urlencode($username) . "&tweet_mode=extended&include_rts=false&exclude_replies=true";
$tokenUrl = "https://api.twitter.com/oauth2/token";

$auth = base64_encode(urlencode($key) . ':' . urlencode($secret));

// BearerToken holen
$getToken = curl_init();
curl_setopt($getToken, CURLOPT_URL, $tokenUrl);
curl_setopt($getToken, CURLOPT_POST, 1);
curl_setopt($getToken, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $auth));
curl_setopt($getToken, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($getToken, CURLOPT_RETURNTRANSFER, 1);

$token = json_decode(curl_exec($getToken));
$token = $token->access_token;
curl_close($getToken);

// Tweets holen
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $quest);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token));
$result = curl_exec($curl);
curl_close($curl);

$tweets = json_decode($result);
like image 103
Tarun Lalwani Avatar answered Nov 04 '22 07:11

Tarun Lalwani