Python Django
Twitter login

How to add Twitter login to a Python Django application

FusionAuth provides a convenient and quick way to implement Twitter login in your Python Django app

How-To > Python Django > Twitter Login

Start implementing Twitter login by setting up FusionAuth

About Twitter login

Configuring Twitter login makes it easy for your users to log in using a Twitter account they already have. Instead of creating an account on your application, users click on a ‘Login with Twitter’ button and log in with their credentials on Twitter. After successfully doing so, they’ll arrive back at your site.

In this doc, you’ll learn how to add Twitter login functionality to your Python Django application. You’ll use FusionAuth to easily add Twitter login.

1. Configure FusionAuth

First, set up FusionAuth and a sample Python Django application. Please follow this tutorial to get that sample application set up.

All done? Great.

Let’s get Twitter login set up for your Python Django application.

2. Enable Twitter login

Now, let’s enable Twitter login using FusionAuth. Create another FusionAuth script. To set up Twitter login, all you need to do is tweak FusionAuth configuration.

Go back to the fusionauth-setup directory:

cd ../setup-fusionauth

You can use the same client library and API key as you used when setting up FusionAuth and the sample Python Django application. But here, you are configuring a different aspect, namely Twitter login.

Put the below code in

from fusionauth.fusionauth_client import FusionAuthClient import os import sys APPLICATION_ID = "e9fdb985-9173-4e01-9d73-ac2d60d1dc8e"; # You must supply your API key api_key_name = 'fusionauth_api_key' api_key = os.getenv(api_key_name) if not api_key: sys.exit("please set api key in the '" + api_key_name + "' environment variable") client = FusionAuthClient(api_key, 'http://localhost:9011') twitter_identity_provider = {} twitter_identity_provider["enabled"] = True twitter_identity_provider["type"] = "Twitter" twitter_identity_provider["buttonText"] = "Login With Twitter" twitter_identity_provider["consumerKey"] = "change-this-in-production-to-be-a-real-twitter-key" twitter_identity_provider["consumerSecret"] = "change-this-in-production-to-be-a-real-twitter-secret" twitter_identity_provider["applicationConfiguration"] = {} twitter_identity_provider["applicationConfiguration"][APPLICATION_ID] = {"enabled":True} client_response = client.create_identity_provider({"identityProvider": twitter_identity_provider}) if not client_response.was_successful(): sys.exit("couldn't add twitter login "+ str(client_response.error_response))

Run this code:

fusionauth_api_key=<your API key> python

You are all done! With this configuration code, you’ve enabled Twitter login for your Python Django application.

Manual configuration of Twitter login

You can also configure Twitter login manually by logging into the FusionAuth admin user interface and navigating to settings, then identity providers, then add a new Twitter identity provider. Configure it, and make sure you enable it for your application.

Manually setting up Twitter login.
Manually setting up Twitter login.

3. Test Out Twitter login

Now, test to see that you have Twitter login enabled for your application. Close any other incognito windows you have open and open a new one. (This ensures you’ve logged out of the application.)

Visit the Python Django application and log in again.

Now, on the login page, you should see a Twitter login button.

What the user sees with Twitter login enabled.
What the user sees with Twitter login enabled.

This is the default look and feel, but you can configure branding using themes.

Next steps for Twitter login

If you want to let your users truly login with Twitter, make sure you set up your Twitter Identity Provider with real client id and client secret values provided by Twitter. Read more in these instructions.

Learn more by visiting the FusionAuth developer docs

Python Django How-Tos

Twitter Login How-Tos