7

I am using "gcloud projects create [project-id]" to create a project, and then "gcloud services enable customsearch.googleapis.com" to enable custom search.

Now I want to create an Api Key but don't see any way to do that. Is it possible?

Thanks

5 Answers 5

5

It appears that you have to create the API key via the GCP Console, however there is a Feature Request for this functionality to be provided in the future so that API keys can be created using gcloud. There is not ETA but any progress can be followed on the above link. For now you can use the GCP Console:

Towards the bottom of this documentation link on the ‘API key’ section you can click on ‘GET A KEY’. A new window will pop up asking you to select your project. Select the project you want the key for and click Next. A new API key will be created for your project.

You can manage your API keys and improve their security via the API Console Credentials menu which can also be used to create API keys.

You should also read the following documentation link which explains a bit more about the pros and cons of using API keys and their limitations which can be overcome using Service Accounts.

Sign up to request clarification or add additional context in comments.

Comments

5

There is a gcloud alpha command available currently.

https://cloud.google.com/sdk/gcloud/reference/alpha/services/api-keys/create

gcloud alpha services api-keys create --display-name="test name"

Check out the examples.

enter image description here

1 Comment

It appears this is now in beta so: gcloud beta services api-keys create --display-name="test name" would also work and is likely to be more stable
2

According to this article, there's an unpublished API for creating API-key.

First you login

gcloud auth login

Then get the access token

gcloud auth print-access-token

Then use the token to list current keys.

curl https://apikeys.googleapis.com/v1/projects/{PROJECT_ID}/apiKeys?access_token={token}

{
  "keys": [
    {
      "keyId": "7d0e2b99-c30c-4b42-bc75",
      "displayName": "API key 1",
      "currentKey": "AIzaSyCT2zcnW0G6dS1N53...",
      "createTime": "2020-05-17T14:09:02.373Z",
      "createdBy": "[email protected]",
      "androidKeyDetails": {}
    }
  ]
}

And create a new API key.

curl -X POST https://apikeys.googleapis.com/v1/projects/{PROJECT_ID}/apiKeys?access_token={token}

{
  "keyId": "0e00ca12-2198-46df-8635",
  "currentKey": "AIzaSyA4JSL7_wt7rVhOV...",
  "createTime": "2020-05-17T16:22:04.941Z",
  "createdBy": "[email protected]",
  "browserKeyDetails": {}
}

Comments

0

As far as I understood there's currently two methods to authenticate for API access:

  • the "legacy" API keys
  • the newer service accounts

But there is limitations that create kind of a dead-lock:

  • legacy API keys can not be created using the gcloud api
  • service accounts can be created using the gcloud api, but need roles&permissions assigned. there is currently no way to assign rights for the very common web apis (f.e. maps, location etc. https://developers.google.com/maps/premium/overview#client-id)

1 Comment

can you please add some example code to your answer so that this answer stays useful even if the content of the linked site changes.
-1

It is now possible using gcloud:

 gcloud beta iam service-accounts keys create OUTPUT-FILE --iam-account=IAM_ACCOUNT [optional flags]

You may need to gcloud components install beta first.

1 Comment

This is definitely valuable, but the command is to create a key for a service account - not to create an API key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.