Google Cloud Rest Auth
gcloud-rest-auth (part of the gcloud-aio project) provides asynchronous Python clients for Google Cloud Platform authentication. It leverages aiohttp for non-blocking I/O, offering an efficient way to interact with Google Cloud services in async applications. The current version is 5.4.4, with releases occurring as needed across its various sub-packages.
Warnings
- breaking Python 3.9 support was dropped in `gcloud-rest-auth` version 5.4.4. Projects running on Python 3.9 will need to upgrade to Python 3.10 or newer.
- gotcha The PyPI package name `gcloud-rest-auth` does not directly map to the import path. The correct import is `from gcloud_aio.auth import AioAuth`, reflecting its structure within the `gcloud-aio` monorepo.
- gotcha This library provides asynchronous clients built on `aiohttp`. If you are working in a synchronous application or using a different HTTP client library, `gcloud-rest-auth` might not be the right fit.
- gotcha Version 5.4.4 fixed an issue where `auto_decompress` settings might have been unexpectedly overwritten or behaved incorrectly when passed as `None`. Older versions might exhibit subtle issues related to HTTP content decompression.
Install
-
pip install gcloud-rest-auth
Imports
- AioAuth
from gcloud_aio.auth import AioAuth
Quickstart
import asyncio
import os
from gcloud_aio.auth import AioAuth
async def main():
# AioAuth will automatically find credentials (e.g., from GOOGLE_APPLICATION_CREDENTIALS, gcloud CLI, metadata server)
# For local development, ensure GOOGLE_APPLICATION_CREDENTIALS or gcloud is configured.
# Example: export GOOGLE_APPLICATION_CREDENTIALS='/path/to/your/key.json'
async with AioAuth() as client:
try:
token = await client.get_access_token()
print(f"Successfully obtained access token: {token[:20]}...")
# You can now use this token for authenticated requests to Google Cloud APIs
except Exception as e:
print(f"Failed to obtain access token: {e}")
print("Please ensure your Google Cloud credentials are set up correctly.")
if __name__ == "__main__":
asyncio.run(main())