Skip to content

Instantly share code, notes, and snippets.

@iCorv
Created April 13, 2020 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iCorv/e70bd76844e9b6e3c95c9eb21669e82b to your computer and use it in GitHub Desktop.
Save iCorv/e70bd76844e9b6e3c95c9eb21669e82b to your computer and use it in GitHub Desktop.
How to generate google access tokens with a service account
import google.auth
from google.auth.transport import requests
# check the scopes you need at https://developers.google.com/oauthplayground in the api list
SCOPES = ['https://www.googleapis.com/auth/devstorage.full_control']
# env variable GOOGLE_APPLICATION_CREDENTIALS has to be set with service account key.json!
credentials, project_id = google.auth.default(scopes=SCOPES)
http_request = requests.Request()
def get_access_token():
credentials.refresh(http_request)
return credentials.token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment