A great deal of the available information regarding the use of Twitter and Python is outdated. The Twitter API has undergone several major revisions in the last few years, and many of the available tutorials now only lead to frustration.
Not only has the API undergone major revisions, but there are multiple supported versions of the API. Some methods referenced by online tutorials will only work with certain other methods!
My hope for this series is to provide a clear and concise tutorial for connecting to the Twitter API using Tweepy and Python.
In order to connect to the Twitter API, your account must be provisioned for Developer access. This is a free service, at the basic level, but does require additional setup. That will be the focus of this first blog post.
You will need:
- A Twitter account with a verified email address and verified phone number
- Developer access to that account
- A Python IDE (I use Spyder)
This post focuses solely on gaining Developer access, and assumes you already have the account.
While I intend for this tutorial to be quite detailed, I trust that you can handle signing up for a Twitter account on your own. Ensure that you’ve verified your email address and added a phone number to your Twitter account.
Getting Developer Access
After you sign up for an account, go to https://dev.twitter.com to request Developer access to the account. Basic developer/API access is free.
After landing on the Developer page, you’ll need to sign up by clicking the “sign up” button in the top right corner:
Next you’ll need to fill out a form. While it looks like an application form, no subsequent review of your application should be required. You should be granted access right away. I strongly urge you to say “no” when they ask about providing information to the government.
Fill out the form and click “Next”. Read the user agreement on the next page and agree to it by checking the checkbox. Click “Submit”. If you haven’t added a verified phone number to your account, you’ll get this warning:
Otherwise, the next screen you see should be this one:
Pick a name for your app. It must be unique. After you’ve named your app, click “Get keys.”
The API Keys
What you should see now are your API Keys and Bearer Token. Ensure that you save this information somewhere safe and secure. If you lose this information, you’ll need to regenerate it, and if you accidentally share this information someone could use it to access your account:
After saving this information, you’ll be taken to the main Developer Portal. You’ll have one Application listed, under the name you gave it. Click the key beside the gear icon to generate the rest of the information you’ll need:
Click “Generate” to generate the remaining pieces of required information:
Access Token and Access Secret
What you’re generating are the Access Token and Access Token Secret. Much like the API keys, these pieces of information need to be stored securely, or else they’ll need to be regenerated:
Wrapping Up Basic Developer Access
You should now have four pieces of information:
- API Key
- API Secret
- Access Token
- Access Token Secret
With this information, we can use Python to connect to your account using the Twitter API. However, there is one more step required before this access is useful.
Elevated API Access
The Twitter API has different access levels. In order to make full use of the API, you’ll need to apply for Elevated Access. You can read more about the different access levels here:
Signing Up For Elevated Access
In order to request elevated access for your Twitter Developer account, click this link: https://developer.twitter.com/en/portal/products/elevated
The resulting sign-up process will be similar to that which you completed when initially requesting Developer access.
Signing Up For Elevated Access
Most of the form should already be filled out for you. You’ll need to tell Twitter how good you are at coding:
After you’ve done that, Click “Next”.
How you fill out the remainder of the form is up to you. Twitter would like to know what you plan to do with this increased access, and if you’re planning to share the data. Again, I strongly suggest that you tell Twitter that you’re not providing information to the Government.
Submit the form, and if all goes well you should be instantly provided with Elevated Access.