Flask Social Authentication - Github & Twitter (With Sample)
Tags: webdev, flask, opensource, authentication
Published: 2022-01-30 14:06:04

---
title: Flask Social Authentication - Github & Twitter (With Sample)
published: true
description: An open-source project that implements social login for Github and Twitter using Flask and Flask-Dance.
tags: webdev, flask, opensource, authentication
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oytj2th4auay8qejqhw7.jpg
canonical_url: https://blog.appseed.us/flask-social-authentication-github-twitter/
---
Hello Coders!

This article presents an open-source project that implements **social login** for `Github` and `Twitter` using **Flask** and `Flask-Dance` library.  The source code can be downloaded from Github (MIT License) and used in real projects or eLearning activities.

> Thanks for reading! 

- 👉 [Flask Social Login](https://github.com/app-generator/flask-social-login-v2) - source code
- 🎁 Free [support](https://appseed.us/support) via email and [Discord](https://discord.gg/fZC6hup) (1k+ community)

---

![Flask Social Login - Github Authentication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5qff05p8ebhtj9yz0fg.png)

---

## ✨ How to use the product

Once the [source code](https://github.com/app-generator/flask-social-login-v2) is downloaded from the public repository, we 
need to create the OAuth applications on Github and Twitter and use the credentials (ApplicationID and SecretKEY) for project configuration.

---

> 👉 Github Setup - [Create an OAuth App](https://docs.github.com/en/developers/apps/building-oauth-apps/creating-an-oauth-app)

- Sign IN to `Github`
- Access `Settings` -> `Developer Settings` -> `OAuth Apps`
- `Edit` your OAuth App
- Set Callback URL: `https://localhost:5000/login/github/authorized`
- `Generate a secret key` (used in the configuration)

 ---

> 👉 Twitter Setup - [Create an OAuth App](https://developer.twitter.com/en/portal/projects-and-apps)

- Sign IN to `Twitter`
- Access [Developer Section](https://developer.twitter.com/en/portal/projects-and-apps)
- Create a new APP
- Edit User authentication settings
- Set the `OAuth` version: v1 or v2 (recommended)
- Set Callback URL: `https://localhost:5000/login/twitter/authorized`

---

Create a new `.env` in the root of the project based on `.env.sample` and save the credentials provided by Github and Twitter. To compile and start the project, follow the steps:

> 👉 Install modules

```bash
$ virtualenv env
$ source env/bin/activate
$ pip3 install -r requirements.txt
```

---

> 👉 Set up the environment for Flask

```bash
$ export FLASK_APP=run.py
$ export FLASK_ENV=development
```

---

> 👉 Start the app using HTTPS

```bash
$ flask run --cert=adhoc
```

The `--cert=adhoc` argument forces Flask to start using HTTPS protocol, required by OAuth redirects for bot platforms.

If all goes well, we should be able to access the app in the browser, authenticate via Github and Twitter and also inspect the information provided by each platform when `/ping` route is accessed:

![Flask Social Login - Github Authentication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z714nhpgeyvxcmouvb4j.png)

---

> Flask Social Login - Twitter Authentication

![Flask Social Login - Twitter Authentication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6erspll6bxo498wnsudf.png)
 
---

> Github Account info - provided by the `/ping` route 

![Flask Social Login - User Information provided by Github.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1x6p64mlqjn16q1od38g.png)

---

> `Thanks for reading!` For more resources, feel free to access:

- ✨ [Free Starters](https://appseed.us/admin-dashboards/open-source) provided in **Flask**, Django, and `React`
- ✨ Access the [documentation](https://docs.appseed.us/) or ask for [support](https://appseed.us/support)