Skip to content

Instantly share code, notes, and snippets.

@anchitarnav
Created June 11, 2020 14:38
Show Gist options
  • Save anchitarnav/2da3caa5ea73a310d23cb6a0911fae35 to your computer and use it in GitHub Desktop.
Save anchitarnav/2da3caa5ea73a310d23cb6a0911fae35 to your computer and use it in GitHub Desktop.
GCP API call using a credential file for a service account
# Doing an API call using some service account credentials
API_TO_HIT = "https://apikeys.googleapis.com/v2alpha1/projects/your-project-name/keys"
from google.auth.transport.requests import AuthorizedSession
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
filename='/path/to/your_service_account_creds.json',
scopes=['https://www.googleapis.com/auth/cloud-platform']
)
session = AuthorizedSession(credentials)
res = session.get(API_TO_HIT)
print(res, res.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment