Skip to content

Instantly share code, notes, and snippets.

@davxiao
Last active August 16, 2025 05:50
Show Gist options
  • Save davxiao/60fe256979ff1249b5145af78307f972 to your computer and use it in GitHub Desktop.
Save davxiao/60fe256979ff1249b5145af78307f972 to your computer and use it in GitHub Desktop.
Programmatically obtain the GCP access token. The equivalent of CLI $(gcloud auth print-access-token)
## programmatically obtain the GCP access token.
## This is the equivalent of CLI $(gcloud auth print-access-token)
import google.auth
import google.auth.transport.requests
creds, project = google.auth.default()
# creds.valid is False, and creds.token is None
# Need to refresh credentials to populate those
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
# Now you can use creds.token
logging.info("creds.token "+creds.token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment