Created
January 11, 2024 18:01
-
-
Save perryism/5ca8f03a1b2ac2e1d4481436e3587151 to your computer and use it in GitHub Desktop.
Get GCP identity or access token programmatically
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
import google.auth | |
from google.auth.transport.requests import Request | |
import requests | |
def get_credentials() -> str: | |
credentials, _project_id = google.auth.default(scopes=['https://www.googleapis.com/auth/cloud-platform']) | |
request = Request() | |
credentials.refresh(request) | |
return credentials | |
# gcloud auth print-identity-token | |
def get_identity_token() -> str: | |
credentials = get_credentials() | |
return credentials.id_token | |
# gcloud auth print-access-token | |
def get_access_token() -> str: | |
credentials = get_credentials() | |
return credentials.token | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment