shivg
shivg

Reputation: 762

How do I create/manage GCP API keys using Python

I am trying to access Google Cloud API Services using client libraries like Python, however there is nothing I can find from the documentation here. The documentation really confusing to find. Any help please.

What i need is to create API Keys using client library for the customers who can access the API gateway using API keys.

Upvotes: 1

Views: 2407

Answers (3)

guillaume blaquiere
guillaume blaquiere

Reputation: 75990

There is no public API for now to create API keys programmatically. I saw a beta version month ago, but now it's no longer available.

I recently view a video with a new API from Googler, but not publicly documented.

So, you can use this API call (not document, subject to change without notice. Use on your own risk)


curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -X POST https://apikeys.googleapis.com/v1/projects/PROJECT_ID/apiKeys

Upvotes: 1

Jose Luis Delgadillo
Jose Luis Delgadillo

Reputation: 2468

According to the official documentation, If you are using an API key for authentication, you must first enable API key support for your service, then to identify a service that sends requests to your API, you use a service account.

Having said that, you can create a service account key using the Cloud Console, the gcloud tool, the serviceAccounts.keys.create() method, the following example is included in the documentation to create the service account key with Python.

import os

from google.oauth2 import service_account
import googleapiclient.discovery

def create_key(service_account_email):
    """Creates a key for a service account."""

    credentials = service_account.Credentials.from_service_account_file(
        filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
        scopes=['https://www.googleapis.com/auth/cloud-platform'])

    service = googleapiclient.discovery.build(
        'iam', 'v1', credentials=credentials)

    key = service.projects().serviceAccounts().keys().create(
        name='projects/-/serviceAccounts/' + service_account_email, body={}
        ).execute()

    print('Created key: ' + key['name'])

You can find another example in this link

Upvotes: 1

Vikram Shinde
Vikram Shinde

Reputation: 1028

If you want to use Google's API, better to create service account and use it in Python code. https://cloud.google.com/docs/authentication/production?hl=en

If you have already created service and you want to expose APIs using API Key then you can follow the instruction in the link you mentioned.

Upvotes: 0

Related Questions