HashandSalt / kirby3-twitter

A Twitter plugin forKirby
MIT License
8 stars 4 forks source link

Plugin defaults to API v1, not available to new Twitter dev accounts #7

Closed sebastiangreger closed 2 years ago

sebastiangreger commented 2 years ago

via https://forum.getkirby.com/t/twit-plugin-twitter-feed-help/24183/14

It appears that the TwitterOAuth library, while now supporting v2, still defaults to v1 of the Twitter API. That API version is no longer available to new dev accounts by default.

self::$connection->setApiVersion('2') in the init() method should switch to using the new API, but is that going to cause issues for existing implementations of the plugin down the line, or is the v2 API response even generally compatible with v1?

/cc @JoshApos

sebastiangreger commented 2 years ago

is the v2 API response even generally compatible with v1?

To answer myself here, as I just did a bit of research for another project where I am facing these changes: Yes, the response data formatting and the behaviour of the API differ from the old format: https://developer.twitter.com/en/docs/twitter-api/migrate/data-formats/standard-v1-1-to-v2

Looks like the data is wrapped into an additional data property, and instead of full_text, the complete text is now in property text. Media and other meta data are only returned when according parameters are given in the request.

So this might be a bit more work than "just" adding ->setApiVersion('2'), I'm afraid :worried:

HashandSalt commented 2 years ago

What a pain :( thanks for investigation @sebastiangreger

Honestly, i have no idea when i will heave the time to look into this.

sebastiangreger commented 2 years ago

I've long been planning to put into use and possibly extend this plugin a bit (hence my interest piqued by this issue), but I, too, don't see much room for that at the moment.

Also comes with the question of how any changes to the plugin may affect its current users – would it be necessary to maintain both APIs in the plugin? Oh my, the joy of building on other people's APIs :sob:

HashandSalt commented 2 years ago

Its difficult for me to test as my API key still gets tweets back. I think cos its an old key and qualifies for the fall back. I suspect new users are being forced on to V2 without access to V1 which is why this whole issue occurs.

I guess ill have to make a new dev twitter account and get a new key.

JoshApos commented 2 years ago

Thank you both for everything so far – I guess, for right now, I'll focus on seeing if I can get Twitter to grant me Elevated access 🙂

HashandSalt commented 2 years ago

@JoshApos ive refactored the plugin this afternoon to bring back into working order. Currently it fine with v1.1 of the API and has very basic support for V2. Im struggling with some of the new API calls. Maybe @sebastiangreger can help a bit there if check it in.

But yes, if you gain elevated access, you should have access to both API versions.

HashandSalt commented 2 years ago

@JoshApos please use the version on the dev branch https://github.com/HashandSalt/kirby3-twitter/tree/develop

This has big upgrades to the dependencies and preliminary support for V2 of the api. Im still figuring that out.

Note the new config option in the readme for setting the API version. Please just ue V1.1 of the api for now until ive finished up the support for v2 properly. This should fine if you have elevated access to the API (basically login to the twitter dev portal, create a project and app in the project, you can eleveted access as part of that process)

sebastiangreger commented 2 years ago

@HashandSalt I just checked out the dev version and it seems to work fine for v1.1 – some small compatibility issue with PHP 7.4, but 8.0 works.

A quick test run with v2 revealed some issues with my dev account, that I can't figure out right now (this whole "Projects", "Apps" etc. is mildly confusing at the moment, compared to how straightforward it used to be). I did, however, read a bit into the v2 API spec and maybe this is of help – it seems that we first need to retrieve the user ID from the screen name, and then request the timeline. Something along these lines (proto code, as indeed my API access exited with an error):

$userId = self::$connection->get('users/by/username/' . $screenName, []);
$tweets = self::$connection->get('users/' . $userId . '/tweets', []);
HashandSalt commented 2 years ago

@sebastiangreger Thank! I asked over on the repo for the Auth library and they suggested this, which is basically waht you just said :)

https://github.com/abraham/twitteroauth/issues/1018#issuecomment-991632745

HashandSalt commented 2 years ago

Made some progress, just messing around in a page template. Just need to turn this into functions that allow you to flip from v1.1 to v2

 <?php

use Abraham\TwitterOAuth\TwitterOAuth;

//your credentials, should be passed in via $_ENV or similar, don't hardcode.
$twitter = new TwitterOAuth(
  option('twit.consumerkey'),
  option('twit.consumersecret'),
  option('twit.accesstoken'),
  option('twit.accesstokensecret')
);

$twitter->setApiVersion('2');

$tweetid = 1469448484185067523;
$myid = 815859093273509888;
$screeNname = 'getkirby';

// tweet by id
$result = $twitter->get('tweets', ['ids' => $tweetid]);

// User ID / Name / Username
$result2 = $twitter->get('users', ['ids' => $myid]);

// User ID / Name / Username
$result3 = $twitter->get('users/by/username/' . $screeNname);

// Timeline
$result4 =  $twitter->get('users/'.$myid.'/tweets', [
  'max_results' => 20
]);

dump($result);
dump($result2);
dump($result3);
dump($result4);

?>
roboscripts commented 2 years ago

Hi,

And is there a quick fix to be able to tweet text and text with images?

// Post message with no image $status = $connection->post('statuses/update', array('status' => $tweet_message));

// Post message with image $status = $connection->upload('statuses/update_with_media', array('status' => $tweet_message, 'media[]' => file_get_contents($image)));

I have tried replacing the endpoint with 'tweets' for the text one with now success

Thanks guys

HashandSalt commented 2 years ago

@roboscripts Please create a new issue, as that is a completely different issue.

JoshApos commented 2 years ago

Huge thanks @HashandSalt, the tweets are showing up! 🙂

HashandSalt commented 2 years ago

@JoshApos and @sebastiangreger I just pushed an exciting major rewrite! Now supports both api's and you have a bunch more power now. Refer to the readme in the develop branch for full details.

Not quite sure what to do with the snippets now as the structure of those depends on the API being used, so i think it might be best to leave those out and let people craft those themselves.

HashandSalt commented 2 years ago

Edit: The snippets are back. See the readme :)

HashandSalt commented 2 years ago

New update pushed :)

sebastiangreger commented 2 years ago

Looking great! I still haven't had the patience to go through the Twitter dev account changes (I'm too concerned I might break some of my existing integrations; better not rock the boat when too busy to fix unexpected stuff), but will definitely give this a try at some point :)

JoshApos commented 2 years ago

That's great, thanks for all of your effort with this 🙂