Skip to content

Instantly share code, notes, and snippets.

@leeyspaul
Created November 30, 2021 20:31
Show Gist options
  • Save leeyspaul/5feb36d42a04426656518dc08b531d9e to your computer and use it in GitHub Desktop.
Save leeyspaul/5feb36d42a04426656518dc08b531d9e to your computer and use it in GitHub Desktop.
OAuth2 Google Calendar API Redirect Route Complete
@app.route("/authorize-user")
def auth_user():
"""
Redirects a user to Google's authorization server to show the OAuth
Consent screen and get user consent.
"""
return redirect(authorization_url)
@app.route("/oauth2redirect")
def oauth2_redirect():
"""
The redirect URI that Google hits after user grants access in the OAuth
consent screen where we fetch the access token from the access code given in the
URL and set them in the flask session.
"""
# grabs the URL response from the redirect after auth
authorization_response = request.url
# fetchs the access code from the request url response
# and then exchanges it for the token
flow.fetch_token(authorization_response=authorization_response)
# grab and set credentials into your flask session
# TODO: in production move these credentials to a persistent data store.
credentials = flow.credentials
flask.session['credentials'] = {
'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes}
return flask.redirect(flask.url_for('test_api_request')) # to be written
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment