Skip to content

Instantly share code, notes, and snippets.

Created November 30, 2021 20:31
What would you like to do?
OAuth2 Google Calendar API Redirect Route Complete
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)
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
# 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