Last active
August 16, 2025 05:50
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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