google api machine learning can I use an API KEY?

2019-07-27 12:13发布

问题:

For learning purposes, I'm trying to use the Machine learning (ml) API.

https://cloud.google.com/ml-engine/reference/rest/v1/projects.models/list

I'm not able to identify if this request can be done with an API KEY instead of OAUTH.

I'm using npm package googleapis with this;

ml.projects.models.list({
    key: GCLOUD_API,
    parent: "projects/"+GCLOUD_PROJECT
  }

But always receives this error:

Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential

I've tried replacing key with auth or token. Nothing work.

How can I know if is possible or not to use an api key?

I tried also with curl

'https://ml.googleapis.com/v1/projects/myproject-123456/models?token=my_super_sekret_key'

回答1:

Today I had the same doubt.

Here are GCP Services that support API Keys: https://cloud.google.com/docs/authentication/api-keys and ML API is not included.

You should obtain the access token using OAuth2, so URL POST request will be:

https://ml.googleapis.com/v1/projects/your_project/models?access_token=your_access_token

Works great for me. Same to do predictions.



回答2:

1

You can get the auth token using gcloud:

access_token=$(gcloud auth application-default print-access-token)

and then embed it into the header:

curl --silent \
    -H "Authorization: Bearer $access_token"  \
    -H "Content-Type: application/json" \
    -X POST \
    -d "$request" \
    https://ml.googleapis.com/v1/projects/myproject-123456/models