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