Flask Social Authentication - Github & Twitter

Hello! 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 - Github & Twitter.

✨ How to use the product

Once the source code 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
  • Sign IN to Github
  • Access Settings -> Developer Settings -> OAuth Apps
  • Edit your OAuth App
  • Callback URL: https://localhost:5000/login/github/authorized
  • Generate a secret key (used in the configuration)

πŸ‘‰ Twitter Setup - Create an OAuth App
  • Sign IN to Twitter
  • Access Developer Section
  • Create a new APP
  • Edit User authentication settings
  • Check OAuth version: v1 or v2 (recommended)
  • 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, please follow the steps: Β 

πŸ‘‰ Create a new virtual environment & install dependencies
$ virtualenv env
$ source env/bin/activate
$
$ pip3 install -r requirements.txt
Flask Social Login - Install Modules
πŸ‘‰ Set up the environment for Flask
$ export FLASK_APP=run.py
$ export FLASK_ENV=development
Flask Social Login - Edit the Environment
πŸ‘‰ Start the app using HTTPS
$ flask run --cert=adhoc
Flask Social Login - Start the app

The --cert=adhoc argument forces Flask to start using HTTPS protocol, required by OAuth redirects for both 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
Flask Social Login - Twitter Authentication
Flask Social Login - Twitter Authentication
Github Account info - provided by /ping route
Flask Social Login - User Information provided by Github.

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