Python Friday #110: Accessing Twitter from Python

At Python Friday #100 I asked for topics to cover in this blog series. The request from kiquenet to cover Twitter offers interesting challenges that we will explore over the next weeks. Today we start with the basics and post our first tweet.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Create a developer account

If you want to use code to interact with Twitter, your regular account is not enough. You need a developer account from developer.twitter.com. The registration is the simple part, registering your application can be a challenge. You need to tell Twitter what you want to do and how your application will enrich the Twitter ecosystem.

I suggest you take your time and carefully read the documents on the developer platform. If your answers are not satisfying, your application may be declined, or you may need to answer additional questions. A bit of upfront reading can save you a lot of emails.

 

Create a project

If your application is approved, you can create a project. The project needs a name, a description, and the usage scenario.

 

Create an App

In the project you can create your Apps. The App is the place where the interesting parts happen and where you get the access keys you need to call the API. You can use different environments and I strongly suggest you create an environment for development and a separate one for production. The development environment accepts localhost as a redirect URL, what will significantly improve your development time.

Attention: When you create your App you need to store the generated Keys. Make sure that you not only to save the values but also the name of the keys. It is important to keep your bearer token apart from your API key. Use a .env file to keep your secrets out of your code.

 

Pick a Twitter library

There are many Twitter libraries for Python. Unfortunately, most are outdated. Twitter is currently changing its endpoints and if the last update of the library is more than 6 months in the past, you will have to fight API incompatibilities instead of adding features to your application.

I can recommend Tweepy. The library gets frequent updates and the documentation is up to date. You can install Tweepy with pip:

 

Write your first Tweet

In the documentation of Tweepy you find an example that uses a PIN to authenticate. This is the simplest way to try Tweepy because you do not need a web application that runs on the redirect URL. You can use the code here and a .env file that contains your API keys:

If you run the code, you get an URL that looks something like this:

https://api.twitter.com/oauth/authorize?oauth_token= cPTS***************qm_o

Click on the URL and Twitter should ask you if you want to authorise your App. If you agree, Twitter tries to redirect you to your application. If you have no running application, this will give you an error in the browser. We look at this problem in a later post, for now you can copy the parameter oauth_verifier from the browser (from the last = to the end of the URL):

http://127.0.0.1:6006/callback?oauth_token=cPTS***************qm_o&oauth_verifier=Wn*****************e

You can past the verifier to the running Python program, hit enter and Tweepy will post your Tweet:

Your first Tweet send by Tweepy

If you get a 401 error instead, your application does not have the right to tweet as the user. You need to go back to the developer portal and increase the rights for your application.

 

Next

If the tweet succeeded, we have everything in place to explore the Twitter API. Before we do that, we need to have a closer look at authentication and how we can capture the authentication tokens. Otherwise, you will need to repeat the application authorisation with every call to Twitter.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.