Skip to content

Instantly share code, notes, and snippets.

@MartinHarding
Created March 6, 2018 22:40
Show Gist options
  • Save MartinHarding/6f09588676f9ca911e9d52a87d8adc6f to your computer and use it in GitHub Desktop.
Save MartinHarding/6f09588676f9ca911e9d52a87d8adc6f to your computer and use it in GitHub Desktop.
Flask-Dance Social Login Examples
# Super quick and dirty example of social login with Github, Facebook, Twitter, and Google using Flask-Dance
from flask import Flask, redirect, url_for
from werkzeug.contrib.fixers import ProxyFix
from flask_dance.contrib.github import make_github_blueprint, github
from flask_dance.contrib.twitter import make_twitter_blueprint, twitter
from flask_dance.contrib.facebook import make_facebook_blueprint, facebook
from flask_dance.contrib.google import make_google_blueprint, google
from flask import request
import json
# Setup app
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
app.secret_key = 'supersekrit'
# API Keys/Secrets
gh_key = '<Github Key>'
gh_secret = '<Github Secret>'
tw_key = '<Twitter Key>'
tw_secret = '<Twitter Secret>'
fb_key = '<Facebook Key>'
fb_secret = '<Facebook Secret>'
gg_key = '<Google Key>'
gg_secret = '<Google Secret>'
# HTML helper functions
def link(name):
return '<a href="/login/{}">Login {}</a>'.format(name, name.title())
def pre(response):
formatted = json.dumps(response.json(), indent=4)
return str('<pre>'+formatted+'</pre>')
# Show data or links to login pages
@app.route('/')
def index():
gh_api = '/user'
tw_api = 'account/verify_credentials.json'
fb_api = 'me?fields=id,email'
gg_api = '/oauth2/v2/userinfo'
gh = pre(github.get(gh_api)) if github.authorized else link('github')
tw = pre(twitter.get(tw_api)) if twitter.authorized else link('twitter')
fb = pre(facebook.get(fb_api)) if facebook.authorized else link('facebook')
gg = pre(google.get(gg_api)) if google.authorized else link('google')
return '<br>'.join([gh, tw, fb, gg])
# Github login
blueprint = make_github_blueprint(
client_id=gh_key,
client_secret=gh_secret,
)
app.register_blueprint(blueprint, urlprefix='/login/github')
@app.route('/login/github')
def github_login():
url = '/' if github.authorized else url_for('github.login')
return redirect(url)
# Twitter login
blueprint = make_twitter_blueprint(api_key=tw_key, api_secret=tw_secret)
app.register_blueprint(blueprint, urlprefix='/login/github')
@app.route('/login/twitter')
def twitter_login():
url = '/' if twitter.authorized else url_for('twitter.login')
return redirect(url)
# Facebook login
blueprint = make_facebook_blueprint(client_id=fb_key, client_secret=fb_secret, scope='email')
app.register_blueprint(blueprint, urlprefix='/login/facebook')
@app.route('/login/facebook')
def facebook_login():
url = '/' if facebook.authorized else url_for('facebook.login')
return redirect(url)
# Google login
blueprint = make_google_blueprint(client_id=gg_key, client_secret=gg_secret)
app.register_blueprint(blueprint, urlprefix='/login/google')
@app.route('/login/google')
def google_login():
url = '/' if google.authorized else url_for('google.login')
return redirect(url)
# Run app
if __name__ == '__main__':
app.run(ssl_context='adhoc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment