Using Your Twitter API Key

Below is an embedded version of an iPython notebook I have made publicly available on nbviewer. To download a copy of the code, click on the icon with three horizontal lines at the top right of the notebook (just below this paragraph) and select “Download Notebook.” I hope you find it helpful. If so, please share, and happy coding!




Setting up Access to the Twitter API

search-api_v2

The Twitter API (application programming interface) is your gateway to accessing Twitter data. The image above shows a screenshot of Twitter’s Search API, just one of the key parts of the API you might be interested in. To access any of them you’ll need to have a password. So, in this post I’m going to walk you through getting access to the Twitter API. By the end you’ll have a password that you’ll use in your Python code to access Twitter data.

Sign up for a Twitter Account and Create an App

Most social media platforms follow a similar set of steps that you’ll go through here: you’ll go to the Developer page, set up an ‘app’, and then generate a set of passwords that grant you access to the API.

On Twitter, the first thing you’ll have to do is have a Twitter account. Once you have that, go to Twitter’s ‘developer’ page: https://dev.twitter.com. Once you’re logged into the developer page you’ll then have to create an ‘app’. Click on the My Apps link or go directly to: https://apps.twitter.com This will take you to the following screen, where you can see I have already created three apps. Click on ‘Create New App’.

twitter_developer_home_page_v2

Create your App

You’ll then be taken to the screen shown in the following image. You might be wondering why it’s called an ‘app’ and why you have to create one. The short answer is that Twitter and other social media platforms allow access to their data mainly for developers, or people creating apps that interact with the Twitter data. Academics and researchers are not the main targets but we access the data the same way.

You’ll need to fill in three things as shown in the image. For the ‘Name’ just put in anything you want — I chose ‘ARNOVA2016’ here. As long as it makes sense to you you’re fine. You’ll also have to put in a brief description. Here I typically put in something about academic or not-for-profit research. Finally, you’ll put in a website address (hopefully you have something you can use) and click ‘Create your Twitter Application.’

app_creation_page_v2

Successfully Created App

You’ll then be taken to the following screen, which indicates a successfully created app. Click on Keys and Access Tokens:

app_creation_success_page_v2

Generate Access Tokens

On this screen you’ll see the first two parts of your four-part password: the API KEY and the API SECRET. You still need to generate the final two parts, so click on ‘Regenerate Consumer Key and Secret.’

keys_and_access_tokens_page_v2

Copy the Four Parts of Your Password

You’ll then be taken to the final page as shown in the image below. You now have all four parts to your ‘password’ to accessing the Twitter API: the API KEY, the API SECRET, the ACCESS TOKEN, and the ACCESS SECRET (I’ve pixelated or obscured mine here). Keep these in a safe place — you’ll be using them in any code in which you want to access the Twitter API.

generated_access_tokens_v2

You’re done! You now have your Twitter API library card and are ready to go hunting for data. In an upcoming post I’ll show you how to actually use your password to access the data.