aiogoogle

5.17.0 · active · verified Sat Apr 11

aiogoogle is an asynchronous Google API client for Python, leveraging `aiohttp` for non-blocking I/O. It simplifies interaction with various Google services using service accounts, user credentials, and API keys. The current version is 5.17.0, and it maintains an active release cadence with frequent bug fixes and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `Aiogoogle` using `ServiceAccountCreds` and discover a Google API (Cloud Resource Manager v3). It highlights the `async with` context manager for proper session management. Replace `path/to/your/service_account_key.json` with your actual service account key path and ensure `GOOGLE_APPLICATION_CREDENTIALS_PATH` environment variable is set for authentication. The project listing is commented out as it requires proper permissions and a valid project.

import asyncio
import os
from aiogoogle import Aiogoogle
from aiogoogle.auth.service_account import ServiceAccountCreds

# For simplicity, using a dummy service account key path
# In a real application, ensure this path is secure and correct.
SERVICE_ACCOUNT_KEY_PATH = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS_PATH', 'path/to/your/service_account_key.json')

async def get_google_services():
    if not os.path.exists(SERVICE_ACCOUNT_KEY_PATH): # Dummy check for quickstart
        print(f"Warning: Service account key not found at {SERVICE_ACCOUNT_KEY_PATH}. "
              "Using dummy credentials which will likely fail authentication.")
        # Create dummy creds to allow Aiogoogle instantiation
        creds = ServiceAccountCreds(scopes=['https://www.googleapis.com/auth/cloud-platform'])
    else:
        creds = ServiceAccountCreds.from_file(SERVICE_ACCOUNT_KEY_PATH,
                                              scopes=['https://www.googleapis.com/auth/cloud-platform'])

    async with Aiogoogle(service_account_creds=creds) as aiogoogle:
        # Discover a Google API, e.g., Cloud Resource Manager API
        try:
            cloudresourcemanager_v3 = await aiogoogle.discover('cloudresourcemanager', 'v3')
            print("Successfully discovered Cloud Resource Manager API v3!")

            # Example: List projects (requires appropriate permissions)
            # This call will likely fail without proper authentication and permissions.
            # try:
            #     projects_req = cloudresourcemanager_v3.projects.list(query='state:ACTIVE')
            #     projects_res = await aiogoogle.as_service_account(projects_req)
            #     print("Projects found:", projects_res)
            # except GoogleAPIError as e:
            #     print(f"Error listing projects: {e.reason}")

        except Exception as e:
            print(f"Failed to discover API or an error occurred: {e}")

if __name__ == '__main__':
    asyncio.run(get_google_services())

view raw JSON →