google-auth-library-python
google-auth-library-python copied to clipboard
feat: Added Temporary token based authentication similar to cli --access-token-file
Fixes #1165
This features allows using access token in the sdk without having any of the GCP related credentials.
Example Usage:
- Generate the token using CLI
gcloud auth print-access-token [email protected]
- Generate the token using python.
from google.auth import compute_engine
from google.auth import default, credentials
from google.auth.transport.requests import Request
import google.auth
from google.cloud import compute_v1
def generate_access_token():
creds, project = google.auth.default()
creds.refresh(Request())
return creds.token, creds.expiry
- Copy this access token to a different/local machine or container having no other google credentials.
- Set the required environment variable.
export GOOGLE_TEMPORARY_ACCESS_TOKEN=token copied from above
- Try running the following python code to verify if the credentials are working.
def list_virtual_machines(project_id, creds):
# Create a client for Compute Engine with the provided access token
compute_client = compute_v1.NetworksClient(credentials=creds)
# List VMs
vm_list = compute_client.list(project=project_id)
# Print VM names
print("Virtual Machines:")
for vm in vm_list:
print(vm.name)
os.environ["GOOGLE_TEMPORARY_ACCESS_TOKEN"] = "access token copied from above"
creds, project_id = google.auth.default()
list_virtual_machines("bamboo-weft-375813", creds=creds)
It should work fine till the token is valid.
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
View this failed invocation of the CLA check for more information.
For the most up to date status, view the checks section at the bottom of the pull request.
🤖 I detect that the PR title and the commit message differ and there's only one commit. To use the PR title for the commit history, you can use Github's automerge feature with squashing, or use automerge
label. Good luck human!
-- conventional-commit-lint bot https://conventionalcommits.org/