Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making adjustments to allow operation w/ API 2 #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AndyM84
Copy link

@AndyM84 AndyM84 commented Nov 29, 2022

Adjusted Twitter::request(..) to allow users the ability to query Twitter API v1.1 & v2:

  • Created Twitter::makeApiUrl(..) method for joining versioned API resource paths
  • Added $apiSuffix parameter to Twitter::request(..) for specifying Twitter API version suffix
  • Modified Twitter::request(..) logic to exclude '.json' append for API v2 paths
  • Modified Twitter::request(..) to use Twitter::makeApiUrl(..) for incomplete API paths
  • Modified code-docs for Twitter::request(..) to provide more information
  • Modified readme.md to note that API v2 requests are possible

Tested both of the following requests with my own keys to ensure they worked:

$statuses = $twitter->request('tweets', 'GET', ['ids' => '???'], [], Twitter::API_2_SUFFIX);
$statuses = $twitter->request('statuses/retweets_of_me', 'GET', ['count' => 20]);

@CreativeNative
Copy link

When will this pull request be merged? We need the API V2.

@AndyM84
Copy link
Author

AndyM84 commented Jan 11, 2023

When will this pull request be merged? We need the API V2.

Not sure the answer to that, but as a workaround you can specify the full URL for an API endpoint. That will keep it from adding the .json to the end of the path while also letting you use v2. Not perfect, but it works in a pinch!

@hissy
Copy link

hissy commented Jun 26, 2023

Thanks @AndyM84 , your patched version works as expected with v2 endpoints.

@barart
Copy link

barart commented Aug 10, 2023

When will this pull request be merged? We need the API V2.

Not sure the answer to that, but as a workaround you can specify the full URL for an API endpoint. That will keep it from adding the .json to the end of the path while also letting you use v2. Not perfect, but it works in a pinch!

@AndyM84

Can you clarify how or where do i need to specify the full url for an api endpoint and what is the url? I already have your commit files

Or how can i specify the url or the version? like on this function:

$tweet = $twitter->send('Hello World');

Using it give mi this error: Error: You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product

If i change Twitter.php file public function request () to this:

public function request(string $resource, string $method, array $data = [], array $files = [], string $apiSuffix = self::API_2_SUFFIX)

It return this: Error: Server error #404 with answer

If i also change the Twitter.php file send function lines to this:

public function send(string $message, $mediaPath = null, array $options = []): stdClass
	{
		$mediaIds = [];
		foreach ((array) $mediaPath as $item) {
			$res = $this->request(
				'https://upload.twitter.com/1.1/media/upload.json',
				'POST',
				[],
				['media' => $item]
			);
			$mediaIds[] = $res->media_id_string;
		}
		return $this->request(
			//'statuses/update',
			'tweets',
			'POST',
			$options + ['status' => $message, 'media_ids' => implode(',', $mediaIds) ?: null]
		);
	}

I got this:

Error: Requests with bodies must have content-type of application/json.

So, what is the correct way to use the functions?

@AndyM84
Copy link
Author

AndyM84 commented Aug 10, 2023

You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level.

@barart, the above message makes it sound like you have an issue with your Twitter API account, and not a problem with the library or my changes?

@barart
Copy link

barart commented Aug 10, 2023

@barart, the above message makes it sound like you have an issue with your Twitter API account, and not a problem with the library or my changes?

@AndyM84

No, is not a problem with my twitter account, i think its the library itself it's obsolete, twitter deprecated almost all the 1.1 endpoints, in order to create a tweet you need to use the v2 endpoint, but upload the media with 1.1 endpoint, to search tweets you need to use v2 as well, so, your changes do not work completely for v2, i think that unfortunately the library needs to be fully rewritten

Example:

statuses/update only works on 1.1 but is deprecated

statuses/update do not exists on v2 (404 error)

tweets is the endpoint for create a tweet

Same as that endpoint is the search endpoint

search/tweets endpoint is for 1.1 but dont exists on 2, for 2 it changed the words to this endpoint: tweets/search

Try to use the send, search funcions of the library so you can check the errors,

@AndyM84
Copy link
Author

AndyM84 commented Aug 11, 2023

Maybe if I get some time to work on this I'll look into updating other parts, but I really only did this so I could automate deleting my tens of thousands of tweets when I left the platform after Musk took over.

If you want to take a crack at it, it seems like you would just need to normalize the API endpoints that are used by the various methods so it matches where the new equivalents are at Twitter. Also, the Twitter::send() use of Twitter::request() is a good example of how to provide full-length URL's to the request method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants