I've been using PHP + Twitter API (with library https://github.com/abraham/twitteroauth) for quite some time, but recently they changed the API and introduced the v2. I only use the API for writing / deleting tweets on behalf of an user.
I've filled all the information needed in the new Twitter Developer Portal and I'm able to connect to Twitter using the OAuth flow in my App.
My previous code for tweeting was:
$tw = new TwitterOAuth
(
$apiKey,
$apiSecret,
$accessToken,
$accessSecret
);
$data = ['status' => 'Hello world'];
$response = $tw->post('statuses/update', $data);
print_r($response);
Which no longer works as it returns the error:
Twitter: You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-leve
Which I don't think this error makes much sense, because in the link provided we can see the price-plans:
So the endpoint statuses/update
should work normally in free plans (like mine) but it doesn't. Nevertheless, when converting the code to use the new API v2:
$tw = new TwitterOAuth
(
$apiKey,
$apiSecret,
$accessToken,
$accessSecret
);
$data = ['text' => 'Hello world'];
$response = $tw->post('2/tweets', $data);
print_r($response);
The $response
comes empty, no error, no success, nothing. However, while applying the same concept in Postman it works, the tweet is created.
Is TwitterOAuth library missing something or am I?
Digging the https://github.com/abraham/twitteroauth, more precisely the file under src/TwitterOAuth.php I was able to verify two things:
private const API_VERSION = '1.1';
and inserted into the URL requests.json
, like: api.twitter.com/1.1/some-path.json
This doesn't meet the new v2 documentation.
The solution is to create a function which defines what API version to use (sometimes we must use the 1.1
and others the 2
):
private $apiVersion = '1.1';
public function setApiVersion($version)
{
$this->apiVersion = $version;
return $this;
}
And then replace the function http
:
private function http(
string $method,
string $host,
string $path,
array $parameters,
bool $json
) {
$this->resetLastResponse();
$this->resetAttemptsNumber();
$url = sprintf('%s/%s/%s.json', $host, self::API_VERSION, $path);
$this->response->setApiPath($path);
if (!$json) {
$parameters = $this->cleanUpParameters($parameters);
}
return $this->makeRequests($url, $method, $parameters, $json);
}
with:
private function http(
string $method,
string $host,
string $path,
array $parameters,
bool $json
) {
$this->resetLastResponse();
$this->resetAttemptsNumber();
$url = sprintf('%s/%s/%s', $host, $this->apiVersion, $path . ($this->apiVersion == '1.1' ? '.json' : ''));
$this->response->setApiPath($path);
if (!$json) {
$parameters = $this->cleanUpParameters($parameters);
}
return $this->makeRequests($url, $method, $parameters, $json);
}
Finally, to use the library with the new version:
$tw = new TwitterOAuth
(
$apiKey,
$apiSecret,
$accessToken,
$accessSecret
);
$data = ['text' => 'Hello world'];
$response = $tw->setApiVersion('2')->post('tweets', $data);