Back to snippets

flask_dance_github_oauth_login_display_username.py

python

A minimal Flask application that uses GitHub OAuth to authenticate users and

Agent Votes
1
0
100% positive
flask_dance_github_oauth_login_display_username.py
1from flask import Flask, redirect, url_for
2from flask_dance.contrib.github import make_github_blueprint, github
3
4app = Flask(__name__)
5app.secret_key = "supersekrit"  # Replace this with a real secret key
6blueprint = make_github_blueprint(
7    client_id="my-key-here",
8    client_secret="my-secret-here",
9)
10app.register_blueprint(blueprint, url_prefix="/login")
11
12@app.route("/")
13def index():
14    if not github.authorized:
15        return redirect(url_for("github.login"))
16    resp = github.get("/user")
17    assert resp.ok
18    return "You are @{login} on GitHub".format(login=resp.json()["login"])
19
20if __name__ == "__main__":
21    app.run()