Skip to content

Loading…

Twitter API Version 1.1 Support #166

Merged
merged 4 commits into from

4 participants

@aaronwaldon

The search endpoint is no longer a separate URI (https://dev.twitter.com/docs/api/1.1). Everything else seems to work as expected, as long as oAuth credentials are used. I have tested this with almost all GET endpoints, but not with any POST endpoints.

aaronwaldon added some commits
@aaronwaldon aaronwaldon Updated for use with Twitter API version 1.1
The search endpoint is no longer a separate URI (https://dev.twitter.com/docs/api/1.1). Everything else seems to work as expected, as long as the user authenticates. I have tested this with almost all GET endpoints, but not for any POST endpoints.
0eac35c
@aaronwaldon aaronwaldon Simplified API Version 1.1 Support c3e3357
@jmathai
Owner

Thanks for this. I'm rate limited so I can't get all the unit tests to work. Once I can I'll merge and push.

@aaronwaldon

I don't think all of the unit tests will pass in their current state, because some of the endpoints have changed. For example, the testGetFollowers method of EpiTwitterTest will fail, as the '/statuses/followers' endpoint no longer exists in the 1.1 version of the Twitter API.

@jmathai
Owner

They don't even come close. for now I'll just remap to new endpoints or mark other ones as incomplete if it requires more work than that.

@aaronwaldon

Maybe it would be wise to have different unit tests for different versions of the framework. I could also add in a conditional to handle the old search endpoint for the 1.0 version of the API.

I don't have a ton of time to spare, but I can try to set up 1.0 backwards compatibility for the search endpoint and some new unit tests for the 1.1 version sometime this week.

aaronwaldon added some commits
@aaronwaldon aaronwaldon Updated EpiTwitter for API v1.0 compatibility.
This change allows version 1.1 to work, while still allowing the default (version 1.0) to work as expected.
34ba9c7
@aaronwaldon aaronwaldon Forgot to add the getApiURL method change...
Will use the search version 1 API URI if applicable, but will use the normal API URI otherwise.
8c730de
@aaronwaldon

I updated the proposed code to default to API version 1.0 (not sure how much longer v1.0 will be around, but it remains the default so that all unit tests should still pass).

With these changes, it is quite easy to use the 1.1 version of the API. Once the required classes are included, you can simply instantiate the instance, and specify the API version base API URI:

    $twitter = new EpiTwitter( $consumer_key, $consumer_secret, $oauth_token, $oauth_secret );
    $twitter->useApiVersion( '1.1' );
    $twitter->useApiUrl( 'https://api.twitter.com' );

    //run the method
    $response = $twitter->get( $endpoint, $params );
@tidybit

Hi - I wonder if you could tell me whether it is necessary to update 'public $version' from '1.0'; to '1.1'; in the EpiOAuth.php file. I have grabbed the updated version of the code and am using $twitter->useApiVersion( '1.1' ); in my code and all seems okay but would like to be sure the oAuth version will not cause problems when 1.0 shuts down next week.

Thanks very much for any advice you can give!

@aaronwaldon

Hi @tidybit!

The $twitter->useApiVersion( '1.1' ); line is essentially the same thing as setting public $version from '1' to '1.1'. I just made that change so that the the current unit tests (for the v1.0 API) could pass, although they may not pass here soon anyway due to Twitter discontinuing support. Does that make sense?

@tidybit

Hi @aaronwaldon - thanks for coming back to me so quickly!

The reason I asked the question is that when I changed the version to 1.1 in EpiOAuth.php I had some issues with legacy code so at the moment I have your updated files (thanks!), have set protected $apiVersion = '1.1'; in EpiTwitter.php, am using the $twitter->useApiVersion( '1.1' ); method call and have left public $version = '1.0'; in EpiOauth.php - this seems to work fine with my legacy code.

I think the old API turns off on Tuesday(?) so I guess we'll all find out what will happen then :)

Thanks again for quick answer.

@jfortier

Any updates regarding the 1.1 upgrade? Apparently version 1 is being deprecated May 7th according to their blog.
https://dev.twitter.com/blog/api-v1-retirement-final-dates

@jmathai jmathai merged commit 8c730de into jmathai:master
@jmathai
Owner

Merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 19, 2013
  1. @aaronwaldon

    Updated for use with Twitter API version 1.1

    aaronwaldon committed
    The search endpoint is no longer a separate URI (https://dev.twitter.com/docs/api/1.1). Everything else seems to work as expected, as long as the user authenticates. I have tested this with almost all GET endpoints, but not for any POST endpoints.
  2. @aaronwaldon
Commits on Feb 26, 2013
  1. @aaronwaldon

    Updated EpiTwitter for API v1.0 compatibility.

    aaronwaldon committed
    This change allows version 1.1 to work, while still allowing the default (version 1.0) to work as expected.
  2. @aaronwaldon

    Forgot to add the getApiURL method change...

    aaronwaldon committed
    Will use the search version 1 API URI if applicable, but will use the normal API URI otherwise.
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 8 deletions.
  1. +16 −8 EpiTwitter.php
View
24 EpiTwitter.php
@@ -19,11 +19,14 @@ class EpiTwitter extends EpiOAuth
protected $authorizeUrl = 'https://api.twitter.com/oauth/authorize';
protected $authenticateUrl= 'https://api.twitter.com/oauth/authenticate';
protected $apiUrl = 'http://api.twitter.com';
- protected $apiVersionedUrl= 'http://api.twitter.com';
- protected $searchUrl = 'http://search.twitter.com';
protected $userAgent = 'EpiTwitter (http://github.com/jmathai/twitter-async/tree/)';
protected $apiVersion = '1';
protected $isAsynchronous = false;
+ /**
+ * The Twitter API version 1.0 search URL.
+ * @var string
+ */
+ protected $searchUrl = 'http://search.twitter.com';
/* OAuth methods */
public function delete($endpoint, $params = null)
@@ -57,6 +60,11 @@ public function post_basic($endpoint, $params = null, $username = null, $passwor
return $this->request_basic('POST', $endpoint, $params, $username, $password);
}
+ public function useApiUrl($url = '')
+ {
+ $this->apiUrl = rtrim( $url, '/' );
+ }
+
public function useApiVersion($version = null)
{
$this->apiVersion = $version;
@@ -103,12 +111,12 @@ public function __call($name, $params = null/*, $username, $password*/)
private function getApiUrl($endpoint)
{
- if(preg_match('@^/search[./]?(?=(json|daily|current|weekly))@', $endpoint))
- return "{$this->searchUrl}{$endpoint}";
- elseif(!empty($this->apiVersion))
- return "{$this->apiVersionedUrl}/{$this->apiVersion}{$endpoint}";
- else
- return "{$this->apiUrl}{$endpoint}";
+ if ($this->apiVersion === '1' && preg_match('@^/search[./]?(?=(json|daily|current|weekly))@', $endpoint))
+ {
+ return $this->searchUrl.$endpoint;
+ }
+
+ return $this->apiUrl.'/'.$this->apiVersion.$endpoint;
}
private function request($method, $endpoint, $params = null)
Something went wrong with that request. Please try again.