Skip to content

Instantly share code, notes, and snippets.

@tjkhara
Created September 25, 2020 05:41
Show Gist options
  • Select an option

  • Save tjkhara/c169a8b171b43c8b76d0b3bc396c477f to your computer and use it in GitHub Desktop.

Select an option

Save tjkhara/c169a8b171b43c8b76d0b3bc396c477f to your computer and use it in GitHub Desktop.
Google oauth working code with JP flask course
import os
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true'
os.environ['OATHLIB_RELAX_TOKEN_SCOPE'] = 'true'
################################################
from flask import Flask, redirect, url_for, render_template
from flask_dance.contrib.google import make_google_blueprint, google
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
blueprint = make_google_blueprint(client_id='###',
client_secret='###',
offline=True,
scope=["https://www.googleapis.com/auth/userinfo.email", "openid", "https://www.googleapis.com/auth/userinfo.profile"],
redirect_url='/welcome')
app.register_blueprint(blueprint, url_prefix='/login')
@app.route('/')
def index():
return render_template('home.html')
@app.route('/welcome')
def welcome():
resp = google.get('/oauth2/v3/userinfo')
assert resp.ok, resp.text
email = resp.json()['email']
return render_template('welcome.html', email=email)
@app.route('/login/google')
def login():
if not google.authorized:
return render_template(url_for('google.login'))
resp = google.get('/oauth2/v3/userinfo')
assert resp.ok, resp.text
email = resp.json()['email']
return render_template('welcome.html', email=email)
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment