Google Cloud Async I/O Authentication Client
gcloud-aio-auth is an asyncio-compatible Python client library for Google Cloud Authentication. It provides asynchronous primitives for managing access tokens, IAP tokens, and interacting with IAM. Part of the broader `gcloud-aio` monorepo, it offers async interfaces to various Google Cloud services. The current version is 5.4.4, with releases occurring as part of the actively developed monorepo, often tied to dependency updates or new feature rollouts across components.
Warnings
- breaking Python 3.9 support was dropped in gcloud-aio-auth version 5.4.4. Users on Python 3.9 must upgrade their Python version to 3.10 or newer. [cite: auth-5.4.4 release notes]
- gotcha The `auto_decompress` parameter in `aiohttp.ClientSession` could be inadvertently overwritten by `gcloud-aio-auth` in versions prior to 5.4.4. If you explicitly configure `auto_decompress` on your `ClientSession`, ensure you are on version 5.4.4 or later. [cite: auth-5.4.4 release notes, 7]
- gotcha The `gcloud-aio-auth.Token` class manages credentials specifically for the `gcloud-aio` ecosystem. While related to `google.auth` libraries, direct interoperability by passing `google.auth.default()` credentials objects to `gcloud-aio` clients is not the standard pattern and can lead to issues. Rely on `gcloud-aio.auth.Token` for authentication within `gcloud-aio` clients.
- gotcha When creating `Token` instances (or any `gcloud-aio` client that manages an internal `aiohttp.ClientSession`), it is crucial to properly close them to prevent resource leaks. Use them within an `async with` statement or explicitly call `await token.close()` when done.
Install
-
pip install gcloud-aio-auth
Imports
- Token
from gcloud.aio.auth import Token
- IapToken
from gcloud.aio.auth import IapToken
- IamClient
from gcloud.aio.auth import IamClient
Quickstart
import asyncio
import os
import aiohttp
from gcloud.aio.auth import Token
async def main():
# Attempt to use GOOGLE_APPLICATION_CREDENTIALS environment variable or ADC
# For local development, ensure `gcloud auth application-default login` has been run
# or GOOGLE_APPLICATION_CREDENTIALS points to a service account key file.
service_account_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '')
print("Initializing Token...")
# Initialize Token; it will try to discover credentials if service_file is None.
# Specify necessary scopes for your application.
token = Token(
service_file=service_account_path if service_account_path else None,
scopes=["https://www.googleapis.com/auth/cloud-platform"]
)
async with aiohttp.ClientSession() as session:
try:
# Get an access token (automatically refreshed by the Token instance)
access_token = await token.get()
print(f"Successfully obtained access token (first 10 chars): {access_token[:10]}...")
# Example: Make an authenticated request to a Google Cloud API
# (e.g., list buckets in Google Cloud Storage)
headers = {"Authorization": f"Bearer {access_token}"}
print("Making a dummy authenticated request to Google Cloud Storage API...")
async with session.get(
"https://www.googleapis.com/storage/v1/projects/_/buckets",
headers=headers
) as response:
if response.status == 200:
print(f"Request to GCS successful (status 200). Some buckets (if any): {await response.json()}")
else:
print(f"Request failed with status: {response.status}")
print(f"Response body: {await response.text()}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Ensure the token's internal session is closed
await token.close()
if __name__ == "__main__":
asyncio.run(main())