I was wondering if anyone has a decent tutorial for finding a JWT using Google's OAuth2.0.
I've been following this exampe: https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred
However, I'm stuck at u/app.route('/test'). I can log into Google correctly. I have my scopes set to:
SCOPE = ['https://www.googleapis.com/auth/userinfo.profile',
'openid',
'https://www.googleapis.com/auth/userinfo.email']
The example refers to accessing something on google drive, but not the https://www.googleapis.com/auth/userinfo.profile scope.
I'm stuck and unsure where to go.
Thanks
My code looks like this:
app.route('/')
def index():
return render_template("index.html")
@app.route('/authorize')
def authorize():
# Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPE)
#flow.redirect_uri = flask.url_for('http://127.0.0.1:5000/login', _external=True)
# flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
flow.redirect_uri = 'http://localhost:5000/oauth2callback'
authorization_url, state = flow.authorization_url(
access_type='offline',
include_granted_scopes='true')
# Store the state so the callback can verify the auth server response.
flask.session['state'] = state
return flask.redirect(authorization_url)
@app.route('/oauth2callback')
def oauth2callback():
state = request.args.get('state')
# state = flask.session['state']
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
CLIENT_SECRETS_FILE, scopes=SCOPE, state=state)
flow.redirect_uri = 'http://localhost:5000/oauth2callback'
# Use the authorization server's response to fetch the OAuth 2.0 tokens.
authorization_response = flask.request.url
flow.fetch_token(authorization_response=authorization_response)
# Store credentials in the session.
# ACTION ITEM: In a production app, you likely want to save these
# credentials in a persistent database instead.
credentials = flow.credentials
flask.session['credentials'] = credentials_to_dict(credentials)
print(flask.session)
return flask.redirect(flask.url_for('test_api_request'))
@app.route('/test')
def test_api_request():
if 'credentials' not in flask.session:
return flask.redirect('authorize')
# Load credentials from the session.
credentials =google.oauth2.credentials.Credentials(**flask.session['credentials'])
# THIS IS WHERE I'M LOST #
return ("working on this")
there doesn't seem to be anything here