0

I am trying to send an http post request to my google vertex ai endpoint for prediction. Though I do set the Bearer Token in the request header, the request still fails with the below error:

{
"error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED",
    "details": [
        {
            "@type": "type.googleapis.com/google.rpc.ErrorInfo",
            "reason": "ACCESS_TOKEN_TYPE_UNSUPPORTED",
            "metadata": {
                "service": "aiplatform.googleapis.com",
                "method": "google.cloud.aiplatform.v1.PredictionService.Predict"
            }
        }
    ]
}

}

Since I am making this call from a python backend, I'm not sure if OAuth 2 as suggested in the message would be wise and applicable choice.

The model is already deployed and endpointed test on vertex ai and it worked fine. What I am trying to do is send same prediction task via an http post request using postman and this is what failed.

The request url looks like this:

https://[LOCATION]-aiplatform.googleapis.com/v1/projects/[PROJECT ID]/locations/[LOCATION]/endpoints/[ENDPOINT ID]:predict

Where token bearer is set in the potman authorization tab and instance set in request body.

Prince_SaaS
  • 404
  • 1
  • 5
  • 13
  • Can you share your code for us to reproduce your scenario – Anjela B Jun 21 '22 at 00:31
  • @AnjelaB The model is already deployed and end pointed test on vertex ai and it worked fine. What I am trying to do is send same prediction task via an http post request using postman and this is what failed. – Prince_SaaS Jun 22 '22 at 23:17
  • @AnjelaB I've updated the question with a little more detail. – Prince_SaaS Jun 22 '22 at 23:24

3 Answers3

1

I usually call Vertex AI endpoint this way:

from google.cloud import aiplatform
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/home/user/1a2b3c4d.json"

aip_endpoint_name = (
    f"projects/your-project/locations/us-west1/endpoints/1234567"
)

endpoint = aiplatform.Endpoint(aip_endpoint_name)

Here, you encode the input according to your needs.

def encode_64(input):
    message = input
    message_bytes = message.encode('ascii')
    base64_bytes = base64.b64encode(message_bytes)
    base64_message = base64_bytes.decode('ascii')
    return base64_message

Check the model signature for the input type:

saved_model_cli show --dir /home/jupyter/model --all

Then call the endpoint

instances_list = [{"input_1": {"b64": encode_64("doctor")}}]

instances = [json_format.ParseDict(s, Value()) for s in instances_list]

results = endpoint.predict(instances=instances)
print(results.predictions)

According to the type of the input you are submitting to Vertex AI endpoint (integer, array, string, image), you may have to change the encoding.

razimbres
  • 4,715
  • 5
  • 23
  • 50
0

If you don't want to use oAuth 2.0 for authentication when making http post request, you may want to use Application Default Credentials. In this documentation you can follow the step by step on getting service account key to passing credentials via environment variable.

Anjela B
  • 1,150
  • 1
  • 2
  • 7
  • I did glean some insights from the documentation. Passing credentials using code is an option that worked but only for storage. My question is, what is the expected value for the credential parameter in aiplatform.init? Using the json service account key fails. – Prince_SaaS Jun 25 '22 at 22:03
0

Can you try following:

import subprocess import base64 import requests import json

def get_headers():
  gcloud_access_token = subprocess.check_output("gcloud auth print-access-token".split(' ')).decode().rstrip('\n')
  return {"authorization": "Bearer " + gcloud_access_token}

def send_get_request(uri):
  return requests.get(uri, headers=get_headers(), verify=False)

It should work ff you have gcloud configured (you can do this by using gcloud init) on the machine you are sending request from and have permissions to access Vertex Prediction.

Aleksey Vlasenko
  • 990
  • 9
  • 10