Interacting with the Twitter API using Python

  • Post author:
  • Post category:Python

Interacting with the Twitter API using python

Twitter has a RESTful API to retrieve Tweets for certain queries for use in your applications.

This post will show how we can authorise using our Twitter API key and secret and make requests from this API.

Ouath2 authentication

The first step is authentication.

We start by creating a new app.
https://apps.twitter.com/app/new

Then click on the ‘Keys and Access Tokens’ page and retrieve your Consumer Key and Consumer Secret and we will use these to verify our application.

The twitter API requires a single key that is a string of a base64 encoded version of the two keys separated by a colon so we will create this too.

In [1]:
client_key = 'MyKeyHere'
client_secret = 'MySecretHere'

import base64

key_secret = '{}:{}'.format(client_key, client_secret).encode('ascii')
b64_encoded_key = base64.b64encode(key_secret)
b64_encoded_key = b64_encoded_key.decode('ascii')

The next step is to make a post request to the authentication endpoint to obtain a Bearer Token to be included in subsequent API requests.

In [2]:
import requests

base_url = 'https://api.twitter.com/'
auth_url = '{}oauth2/token'.format(base_url)

auth_headers = {
    'Authorization': 'Basic {}'.format(b64_encoded_key),
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}

auth_data = {
    'grant_type': 'client_credentials'
}

auth_resp = requests.post(auth_url, headers=auth_headers, data=auth_data)
In [3]:
# Check status code okay
auth_resp.status_code
Out[3]:
200
In [4]:
# Keys in data response are token_type (bearer) and access_token (your access token)
auth_resp.json().keys()
Out[4]:
[u'token_type', u'access_token']
In [5]:
access_token = auth_resp.json()['access_token']

Making Queries

The reference documentation for the API can be found at:
https://dev.twitter.com/rest/reference

We will be making a search request for the latest 2 tweets with the terms ‘General Election’.

The options for search parameters can be found at:
https://dev.twitter.com/rest/reference/get/search/tweets

In [6]:
search_headers = {
    'Authorization': 'Bearer {}'.format(access_token)    
}

search_params = {
    'q': 'General Election',
    'result_type': 'recent',
    'count': 2
}

search_url = '{}1.1/search/tweets.json'.format(base_url)

search_resp = requests.get(search_url, headers=search_headers, params=search_params)
In [7]:
search_resp.status_code
Out[7]:
200
In [8]:
tweet_data = search_resp.json()

There is a lot of information that comes with this data, including search metadata, geolocations, twitter author information etc. etc and I encourage you to have an explore.

For now, we will print the text of the tweet.

In [9]:
for x in tweet_data['statuses']:
    print(x['text'] + '\n')
General Election 2017 key target seats: How Theresa May could take 58 Brexit-backing Labour seats https://t.co/lS1Py45ie2

Blair: Theresa May will win General Election https://t.co/rosWx13DqK https://t.co/4Q9Kxkzq0N