0

The documentation on TwitterOAuthAPI.com leaves quite a bit to be desired, to say the least. However, the software does appear to work with an elevated Twitter API Developer account, when used with the API Key and Secret, along with the Access Token and Secret (passed into the TwitterOAuth constructor).

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);

However, with an Essentials account, there's only access to OAuth 2, which Twitter seems to provide a Client ID and Client secret for. If I try to use the Access Token and Access Secret with an Essential account (not elevated or academic), it returns a status saying that an elevated account or academic account is required to use the v1 API).

The developer of TwitterOAuthAPI only currently has an honorable mention of v2 Twitter API and a small code segment, that seems to suggest all we have to do is

$connection->setApiVersion('2');

But this does not work. $connection->get no longer retrieves data, $connection->post no longer posts status updates - just a blank page from what I can tell (no errors or anything, just blank).

So I'm very confused as to how the Client ID and secret are used with this system as I assume, an alternative to the Access Token and Access Token Secret (?).

If anyone knows how to use TwitterOAuthAPI for PHP with the v2 of Twitter API and a non-elevated/academic account, I'd appreciate any insight.

Thanks for your time,

~G

Ginzorf
  • 769
  • 11
  • 19

1 Answers1

3

Recently I was having trouble moving from 1.1 to V2 with the TwitterOAuthAPI library and I finally got it working. It is not mandatory to switch to OAuth 2.0 to use V2, you can still use the consumer keys and oauth token.

$oTwitter = new TwitterOAuth("consumer_key", "consumer_secret", "oauth_token", "oauth_token_secret");

This is the code I used to post a tweet with images in 1.1.

 $aImageParams = array();
    foreach ($aImages as $key => $pathImg) {
        $media = $oTwitter->upload('media/upload', ['media' => $pathImg]);
        array_push($aImageParams, $media->media_id_string);
    }

    $parameters = [
        'status' => 'Hello everyone!!',
        'media_ids' => implode(',', $aImageParams)
    ];
    $status =  $oTwitter->post('statuses/update', $parameters);

Now I do it like this in V2:

 $aImageParams = array();
    foreach ($aImages as $key => $pathImg) {
        $media = $oTwitter->upload('media/upload', ['media' => $pathImg]);
        array_push($aImageParams, $media->media_id_string);
    }

    $parameters =  [
        'text' => 'Hello V2 !!!',
        'media'=> ['media_ids' => $aImageParams]
    ];


    $oTwitter->setApiVersion('2');
    
    $status =  $oTwitter->post('tweets', $parameters,true);

Image uploads are not yet available in V2, so I use the setApiVersion method after uploading with the default version.

Reference: Twitter community post that gave me the key to the solution.

Migration between endpoints from 1.1 to V2.

Post method API with all body parameters in V2.

Alexis
  • 31
  • 3