13.4 Getting Twitter Credentials—Creating an App

Once you have a Twitter developer account, you must obtain credentials for interacting with the Twitter APIs. To do so, you’ll create an app. Each app has separate credentials. To create an app, log into

https://developer.twitter.com

and perform the following steps:

  1. At the top-right of the page, click the drop-down menu for your account and select Apps.

  2. Click Create an app.

  3. In the App name field, specify your app’s name. If you send tweets via the API, this app name will be the tweets’ sender. It also will be shown to users if you create applications that require a user to log in via Twitter. We will not do either in this chapter, so a name like "YourName Test App" is fine for use with this chapter.

  4. In the Application description field, enter a description for your app. When creating Twitter-based apps that will be used by other people, this would describe what your app does. For this chapter, you can use "Learning to use the Twitter API."

  5. In the Website URL field, enter your website. When creating Twitter-based apps, this is supposed to be the website where you host your app. For learning purposes, you can use your Twitter URL: https://twitter.com/YourUserName, where YourUserName is your Twitter account screen name. For example, the URL https://twitter.com/nasa corresponds to the NASA screen name @nasa.

  6. The Tell us how this app will be used field is a description of at least 100 characters that helps Twitter employees understand what your app does. For learning purposes, we entered "I am new to Twitter app development and am simply learning how to use the Twitter APIs for educational purposes."

  7. Leave the remaining fields empty and click Create, then carefully review the (lengthy) developer terms and click Create again.

Getting Your Credentials

After you complete Step 7 above, Twitter displays a web page for managing your app. At the top of the page are App details, Keys and tokens and Permissions tabs. Click the Keys and tokens tab to view your app’s credentials. Initially, the page shows the Consumer API keys—the API key and the API secret key. Click Create to get an access token and access token secret. All four of these will be used to authenticate with Twitter so that you may invoke its APIs.

Storing Your Credentials

As a good practice, do not include your API keys and access tokens (or any other credentials, like usernames and passwords) directly in your source code, as that would expose them to anyone reading the code. You should store your keys in a separate file and never share that file with anyone.3

The code you’ll execute in subsequent sections assumes that you place your consumer key, consumer secret, access token and access token secret values into the file keys.py shown below. You can find this file in the ch13 examples folder:


consumer_key='YourConsumerKey'
consumer_secret='YourConsumerSecret'
access_token='YourAccessToken'
access_token_secret='YourAccessTokenSecret'

Edit this file, replacing YourConsumerKey, YourConsumerSecret, YourAccessToken and YourAccessTokenSecret with your consumer key, consumer secret, access token and access token secret values. Then, save the file.

OAuth 2.0

The consumer key, consumer secret, access token and access token secret are each part of the OAuth 2.0 authentication process4,5—sometimes called the OAuth dance—that Twitter uses to enable access to its APIs. The Tweepy library enables you to provide the consumer key, consumer secret, access token and access token secret and handles the OAuth 2.0 authentication details for you.

tick mark Self Check

  1. (Fill-In) The consumer key, consumer secret, access token and access token secret are each part of the       authentication process that Twitter uses to enable access to its APIs.
    Answer: OAuth 2.0.

  2. (True/False) Once you have a Twitter developer account, you must obtain credentials to interact with APIs. To do so, you’ll create an app. Each app has separate credentials.
    Answer: True.