John Albano
John Albano

Reputation: 199

Create Api Key using gcloud?

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

Upvotes: 7

Views: 5645

Answers (5)

Justin Poehnelt
Justin Poehnelt

Reputation: 3459

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

Upvotes: 5

korakot
korakot

Reputation: 40773

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": {}
}

Upvotes: 2

Jonas Strassel
Jonas Strassel

Reputation: 1

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)

Upvotes: 0

cthulhu
cthulhu

Reputation: 3726

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.

Upvotes: -1

JMD
JMD

Reputation: 598

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.

Upvotes: 5

Related Questions