Google Cloud API Keys

raw JSON →
0.8.0 verified Fri May 01 auth: no python

Client library for the Google Cloud API Keys API, version 0.8.0. Allows you to manage and consume API keys for Google Cloud services. Part of the Google Cloud Python client libraries. Release cadence is irregular, typically monthly or as needed.

pip install google-cloud-api-keys
error ModuleNotFoundError: No module named 'google.cloud.api_keys'
cause Wrong import path. The correct module is `api_keys_v2`.
fix
Replace from google.cloud import api_keys with from google.cloud import api_keys_v2.
error google.api_core.exceptions.NotFound: 404 The resource 'projects/{project}/keys' was not found
cause The parent path for listing keys must include `locations/global`.
fix
Set parent to projects/{project}/locations/global.
error AttributeError: module 'google.cloud.api_keys_v2' has no attribute 'ApiKeysClient'
cause Importing the module but not the client class.
fix
Use from google.cloud import api_keys_v2 then client = api_keys_v2.ApiKeysClient().
error TypeError: __init__() got an unexpected keyword argument 'transport'
cause Using an older version where transport parameter was not supported.
fix
Upgrade to latest version: pip install --upgrade google-cloud-api-keys.
gotcha The import path is `google.cloud.api_keys_v2`, not `google.cloud.apikeys` or `google.cloud.api_keys`. The underscore between `api` and `keys` is part of the module name.
fix Use `from google.cloud import api_keys_v2`.
gotcha The client constructor expects a `credentials` argument if you aren't using Application Default Credentials (ADC). Many users assume it auto-discovers, but if ADC is not set, you must pass `credentials` explicitly.
fix Provide credentials via `google.auth.default()` or use a service account key file with `os.environ['GOOGLE_APPLICATION_CREDENTIALS']`.
gotcha The `parent` path for keys is `projects/{project}/locations/global`. Forgetting `locations/global` causes a 404 error.
fix Always include `locations/global` in the parent path.

Basic usage: list all API keys for a GCP project. Requires a service account key or ADC set up.

import os
from google.cloud import api_keys_v2
from google.cloud.api_keys_v2 import Key, OperationMetadata
from google.cloud.api_keys_v2.services.api_keys.transports import ApiKeysGrpcTransport

def quickstart():
    client = api_keys_v2.ApiKeysClient()
    parent = f"projects/{os.environ.get('GOOGLE_CLOUD_PROJECT', 'my-project')}/locations/global"
    # List API keys
    request = api_keys_v2.ListKeysRequest(parent=parent)
    page_result = client.list_keys(request=request)
    for response in page_result:
        print(f"Key: {response.name}")

if __name__ == "__main__":
    quickstart()