Search code examples
pythondjangogoogle-cloud-platformgoogle-oauthgoogle-cloud-vertex-ai

Google Vertex AI Prediction API Authentication


I have written a Django-Web app that takes in user input from a ModelForm as a Python Dict, then passes into Django backend to make prediction with Google Vertex AI Prediction API, then return the prediction to the web frontend for user.

I have obtained a Service Account Key (JSON) for getting the credentials needed.
However I am still facing 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential

The same code worked initially dated in 14/7/2023, yet it stopped working as of two days ago.

from google.oauth2 import service_account
from google.cloud import aiplatform
from google.protobuf import json_format
from google.protobuf.struct_pb2 import Value


def model_predict(json_data):

    key_path = 'path-to-service-account-key.json'

    # Create a credentials object using your service account key file
    credentials = service_account.Credentials.from_service_account_file(
        key_path,
        scopes=['https://www.googleapis.com/auth/cloud-platform']
    )

    # Deployed Endpoint model configs
    project_id = "project_id"
    endpoint_id = "endpoint_id"
    instance_dict = json_data 
    location = "us-central1"
    api_endpoint = "us-central1-aiplatform.googleapis.com"

    # The AI Platform services require regional API endpoints.
    client_options = {"api_endpoint": api_endpoint}
    
    # Initialise client that will be used to create and send requests.
    client = aiplatform.gapic.PredictionServiceClient(credentials=credentials, client_options=client_options)

    # Pass required configs and instance into AI Client
    instance = json_format.ParseDict(instance_dict, Value())
    instances = [instance]
    parameters_dict = {}
    parameters = json_format.ParseDict(parameters_dict, Value())

    endpoint = client.endpoint_path(
        project = project_id, location=location, endpoint=endpoint_id
    )
    response = client.predict(
        endpoint=endpoint, instances=instances, parameters=parameters
    )
   
    predictions = response.predictions

    for i in predictions:
        result= dict(i)
    
    return result

I wonder if service account key only produces valid OAuth 2 Token for a limited period of time, or is there anything wrong with my code.


Solution

  • Based on the error message 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. the request you made had invalid authentication credentials. Expected authentication credentials include an OAuth 2 access token. The specific error code is "UNAUTHENTICATED," meaning the request was not successfully authenticated.

    Based on the documentation “Create authorization credentials” reviewing your authentication process and ensuring that you are providing the correct authentication credentials. You may need to generate a valid OAuth 2 access token or use the appropriate login cookie for authentication.

    The error code “ 401” is indicating, the user is not authorized to make the request. The authorization credentials provided for the request are invalid. Check the value of the Authorization HTTP request header. For detailed information refer to the documentation