Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Google API OAuth 2.0 Authorization Sample in Python 3 (with requests-oauthlib)
from requests_oauthlib import OAuth2Session
# Credentials you get from registering a new application
client_id = "<the id you get from google>"
client_secret = "<the secret you get from google>"
# OAuth endpoints given in the Google API documentation
authorization_base_url = ""
token_url = ""
scope = [
google = OAuth2Session(client_id, scope=scope, redirect_uri="oob")
# Redirect user to Google for authorization
authorization_url, state = google.authorization_url(authorization_base_url,
# offline for refresh token
# force to always make user click authorize
access_type="offline", prompt="select_account")
print("Please go here and authorize:", authorization_url)
# Get the authorization verifier code from the callback url
code = input("Paste the authorization code: ")
print() # New line after the input line for the next print
# Fetch the access token
google.fetch_token(token_url, client_secret=client_secret,
# Print the token
# Fetch a protected resource, i.e. user profile
r = google.get("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment