Working with Twitter API version 1.1

Twitter have migrated its API platform form version 1 to 1.1 which required authentication in order to access the data from twitter. Once you login to twitter using Oauth by supplying Api Key and Api Secret you will get a access token which use to make a authorized call to Twitter’s API.

Lets see how you can make your own Twitter API Library in Python using urllib2 and json library that call Twitter’s API Internally. Few steps we have to perform In order to get the Twitter’s API access.

Create a Apps

You have to create a Twitter App that will ask you few informations and generates the API and Secret Key. Visit https://apps.twitter.com and login with your twitter account.

Create a Twitter App
Create a Twitter App

You can Test and generate  the signature for the different services for Twitter OAuth API. you can get the list of API provided by Twitter from API Console.

Well It is not full updated, but provides a testing for different version such API 1 and API 1.1. Next thing after setting up an application is to Obtaining access tokens that will used to make an API request.

We can make a service endpoints for our API as below

API_VERSION = '1.1'
API_ENDPOINT = 'https://api.twitter.com'
REQUEST_TOKEN_URL = '%s/oauth2/token' % API_ENDPOINT
REQUEST_FAVORITE_LIST = '%s/%s/favorites/list.json' % (API_ENDPOINT, API_VERSION)
REQUEST_TWEET_LIST = '%s/%s/statuses/user_timeline.json' % (API_ENDPOINT, API_VERSION)

Obtaining access tokens

In order to make authorized calls to Twitter’s APIs, your application must first obtain an OAuth access token on behalf of a Twitter user or you could issue Application-only authenticated requests when user context is not required. The way you will obtain such tokens will depend on your use case. Get details if you want to access Twitter other then Application-only authentication method.

    def connect(self):
        '''
        connect to twiter api end-point https://api.twitter.com/oauth2/token
        and obtain an oauth token
        '''
        bearer_token = '%s:%s' % (self._api_key, self._api_secret)
        encoded_bearer_token = base64.b64encode(bearer_token.encode('ascii'))
        request = Request(REQUEST_TOKEN_URL)
        request.add_header('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
        request.add_header('Authorization', 'Basic %s' % encoded_bearer_token.decode('utf-8'))
        request.add_data('grant_type=client_credentials'.encode('ascii'))

        try:
            response = urlopen(request)
        except urllib2.HTTPError, e:
            logging.error('HTTPError = ' + str(e.code))
        except urllib2.URLError, e:
            logging.error('URLError = ' + str(e.reason))
        except httplib.HTTPException, e:
            logging.error('HTTPException')
        except Exception:
            import traceback
            logging.error('generic exception: ' + traceback.format_exc())

        raw_data = response.read().decode('utf-8')
        data = json.loads(raw_data)
        return data['access_token']

Get Favorite Tweet

Once you success with obtaining a token you can make call to any Twitter API. You need to get the correct service endpoint from where you can get the favorite list, as we have obtain token for Application-only authenticated requests it is not a user context we need to pass Screen name.

    def _execute(self, url, params):
        params_encode = urllib.urlencode(params)
        full_url = "%s?%s" % (url, params_encode)

        request = Request(full_url)
        request.add_header('Authorization', 'Bearer %s' % self._token)
        try:
            response = urlopen(request)
        except urllib2.HTTPError, e:
            logging.error('HTTPError = ' + str(e.code))
        except urllib2.URLError, e:
            logging.error('URLError = ' + str(e.reason))
        except httplib.HTTPException, e:
            logging.error('HTTPException')
        except Exception:
            import traceback
            logging.error('generic exception: ' + traceback.format_exc())
        raw_data = response.read().decode('utf-8')
        data = json.loads(raw_data)
        return data

    def getFavourites(self, screen_name, count=20):
        params = {'count': count, 'screen_name':screen_name}
        data = self._execute(REQUEST_FAVORITE_LIST, params)
        return data

Users Tweet

Call with the same token to another service point with different arguments, and we can get the user’s tweets.

    def getTweets(self, screen_name):
        params = {'screen_name':screen_name}
        data = self._execute(REQUEST_TWEET_LIST, params)
        return data

Almost all the API’s method called with GET method and returns the JSON data when they called, We can also update the data on twitter using Twitter API too which can be call using the POST method.

Limits per window by resource

Rate limit window duration is currently 15 minutes long. Curious how rate limiting works in API v1.1? Read REST API Rate Limiting in v1.1.

Source Code

import base64
import urllib
import json
import urllib2
import httplib
import logging
from urllib2 import Request, urlopen

API_VERSION = '1.1'
API_ENDPOINT = 'https://api.twitter.com'
REQUEST_TOKEN_URL = '%s/oauth2/token' % API_ENDPOINT
REQUEST_FAVORITE_LIST = '%s/%s/favorites/list.json' % (API_ENDPOINT, API_VERSION)
REQUEST_TWEET_LIST = '%s/%s/statuses/user_timeline.json' % (API_ENDPOINT, API_VERSION)

class TwitterAPI(object):

    def __init__(self, api_key, api_secret, token=None):
        self._api_key = api_key
        self._api_secret = api_secret

        if token:
            self._token = token
        else:
            self._token = self.connect()

        logging.info('Connected to twitter')

    def connect(self):
        '''
        connect to twiter api end-point https://api.twitter.com/oauth2/token
        and obtain an oauth token
        '''
        bearer_token = '%s:%s' % (self._api_key, self._api_secret)
        encoded_bearer_token = base64.b64encode(bearer_token.encode('ascii'))
        request = Request(REQUEST_TOKEN_URL)
        request.add_header('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
        request.add_header('Authorization', 'Basic %s' % encoded_bearer_token.decode('utf-8'))
        request.add_data('grant_type=client_credentials'.encode('ascii'))

        try:
            response = urlopen(request)
        except urllib2.HTTPError, e:
            logging.error('HTTPError = ' + str(e.code))
        except urllib2.URLError, e:
            logging.error('URLError = ' + str(e.reason))
        except httplib.HTTPException, e:
            logging.error('HTTPException')
        except Exception:
            import traceback
            logging.error('generic exception: ' + traceback.format_exc())

        raw_data = response.read().decode('utf-8')
        data = json.loads(raw_data)
        return data['access_token']

    def _execute(self, url, params):
        params_encode = urllib.urlencode(params)
        full_url = "%s?%s" % (url, params_encode)

        request = Request(full_url)
        request.add_header('Authorization', 'Bearer %s' % self._token)
        try:
            response = urlopen(request)
        except urllib2.HTTPError, e:
            logging.error('HTTPError = ' + str(e.code))
        except urllib2.URLError, e:
            logging.error('URLError = ' + str(e.reason))
        except httplib.HTTPException, e:
            logging.error('HTTPException')
        except Exception:
            import traceback
            logging.error('generic exception: ' + traceback.format_exc())
        raw_data = response.read().decode('utf-8')
        data = json.loads(raw_data)
        return data

    def getFavourites(self, screen_name, count=20):
        params = {'count': count, 'screen_name':screen_name}
        data = self._execute(REQUEST_FAVORITE_LIST, params)
        return data

    def getTweets(self, screen_name):
        params = {'screen_name':screen_name}
        data = self._execute(REQUEST_TWEET_LIST, params)
        return data

twitter_api_key = 'your api key'
twitter_api_secret = 'your api secret key'
api = TwitterAPI(twitter_api_key, twitter_api_secret)
api.getFavourites('mantavyagajjar')
api.getTweets('mantavyagajjar')