Tim
Tim

Reputation: 61

Google AI Platform Online Prediction - Permission Denied when using REST API

I have created a version of a machine learning model (scikit-learn house prices example) on the Google Cloud AI Platform. I am now following the instructions on this page to test it with online predictions. The prediction is returned perfectly when I request through gcloud, however when I try to use REST API with curl I receive a "Permission Denied" 403 error. My request and the error response are below (I replaced the PROJECT_ID, MODEL_NAME and VERSION_NAME as needed in my request):

REQUEST:

curl -X POST -H "Content-Type: application/json" -d @input.json \
-H "Authorization: Bearer `gcloud auth print-access-token`" \
"https://ml.googleapis.com/v1/projects/${PROJECT_ID}/models/${MODEL_NAME}/versions/${VERSION_NAME}:predict"

ERROR:

{
  "error": {
    "code": 403,
    "message": "Permission denied on resource project #271903.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": "https://console.developers.google.com/project/271903/apiui/credential"
          }
        ]
      }
    ]
  }
}

As I understand permission should be granted through the access token requested with gcloud auth print-access-token. I have also tried entering that command separately and copying in the token manually, but I receive the same error. I have also tried including the json data directly in the request.

Does anyone know this might be happening? Any help is really appreciated!

Upvotes: 3

Views: 6923

Answers (2)

Jay
Jay

Reputation: 2069

I resolved the issue as follows:

  1. Enable the Cloud Resource Manager API by typing in your terminal:
gcloud services enable cloudresourcemanager.googleapis.com
  1. Re-initialize Google Cloud CLI by running the following command:
gcloud init
  1. Login again
gcloud auth login

Done.

REST API cURL Example

curl \
-X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://us-central1-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/us-central1/publishers/google/models/text-bison:predict -d \
$'{
  "instances": [
    { "prompt": "Give me ten interview questions for the role of program manager."}
  ],
  "parameters": {
    "temperature": 0.2,
    "maxOutputTokens": 256,
    "topK": 40,
    "topP": 0.95
  }
}'

Upvotes: 2

Tim
Tim

Reputation: 61

In case this is useful for anyone else, it was my mistake in the request. I replaced the parameters inside the curly brackets rather than replacing the whole variable (between the forward slashes). Correcting this solves the problem and prediction now works.

Upvotes: 3

Related Questions