Login With Twitter in Python — A 2020 Guide

Recently while working on a project I came across an issue with trying to implement Twitter Integrations into my Flask app. All of the guides on Google were breaking or just didn’t fit in with my application. I wanted a simple way to interface with Twitter’s OAuth1 API but everything was giving me errors.

NOTE: This guide assumes you have setup a Twitter Application and have your consumer keys and callback URLs set. If you do not know how to do this please refer to this guide.

Finally, I decided to try something myself, I looked into requests_oauthlib and realized it would be much easier to just use their Client class to generate signatures and implement with my Flask app. I couldn’t find any examples that were easy to understand or read as far as the code went so I wanted to share how I ended up implementing this into my application.

First the libraries I used in my implementation. flask is the library I’m using in order to create my main webserver and viewable routes. We don’t use much apart from the redirect function from it in this. flask_restful is the REST library I’m using in order to easily create a JSON based API. The endpoints we create in this are RESTful API Endpoints that inherit the Resource class from flask_restful. requests_oauthlib is the OAuth library I use in order to generate the OAuth headers to authenticate with Twitter’s API. We will also be using the requests library in order to make requests server side to Twitter.

First you need to setup a flask app. Lets make a folder called app first:

Now from within the app/ directory we will create a file called __init__.py :

This is all we need to do for now in the __init__.py file. Next we will start to create our endpoints for Twitter. This assumes you have setup a Twitter Application and you have your consumer keys.

Next we create our endpoint. For this we can just create a file called twitter.py or really whatever you’d like. Next we add:

This is our authentication endpoint, when somebody attempts to hit this it will redirect them to the Twitter login page which will then send them to our Callback URL which you should have setup in your Twitter App’s dashboard.

Next we need to add the logic for handling our callback URL. We can add this to the same file as the TwitterAuthenticate class.

This TwitterCallback class will parse and handle the OAuth tokens in order to access the user’s account info. You can do whatever you want with this info whether that be storing it into a DB User Model or making requests with Tweepy!

The last step is adding these endpoints to your __init__.py file we created at the beginning! At the bottom of the file add this:

Our final twitter.py should look like this:

Our final __init__.py should look something like:

I hope you find this guide helpful and have a fun time integrating with Twitter’s User API!

You can find a GitHub Gist here with the files shown above.

Follow me on Twitter and GitHub for more interesting programming stuff!

QA Engineer @ Reggora | iOS and Python | Tweets are my own opinion and do not reflect my employer | @GetSileo @SecuraRepo @psty_io | 19