Google Vertex AI预测API身份验证

huangapple go评论76阅读模式
英文:

Google Vertex AI Prediction API Authentication

问题

我已经编写了一个使用Django-Web应用程序,该应用程序将用户输入作为ModelFormPython Dict接收,然后传递给Django后端,使用Google Vertex AI Prediction API进行预测,然后将预测结果返回给Web前端用户。

我已经获取了一个用于获取所需凭据的Service Account Key (JSON)。然而,我仍然遇到了401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential的问题。

相同的代码最初在2023年7月14日运行正常,但在两天前停止工作。

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'

    # 使用服务账号密钥文件创建凭据对象
    credentials = service_account.Credentials.from_service_account_file(
        key_path,
        scopes=['https://www.googleapis.com/auth/cloud-platform']
    )

    # 部署的终端模型配置
    project_id = "project_id"
    endpoint_id = "endpoint_id"
    instance_dict = json_data 
    location = "us-central1"
    api_endpoint = "us-central1-aiplatform.googleapis.com"

    # AI Platform服务需要区域性API端点。
    client_options = {"api_endpoint": api_endpoint}
    
    # 初始化用于创建和发送请求的客户端。
    client = aiplatform.gapic.PredictionServiceClient(credentials=credentials, client_options=client_options)

    # 将所需配置和实例传递给AI客户端
    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

我想知道服务账号密钥是否只能在有限的时间内生成有效的OAuth 2令牌,或者我的代码是否有问题。

英文:

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.

答案1

得分: 1

根据错误信息401请求具有无效的身份验证凭据。预期的OAuth 2访问令牌、登录cookie或其他有效的身份验证凭据。您所发出的请求具有无效的身份验证凭据。预期的身份验证凭据包括OAuth 2访问令牌。具体的错误代码是"UNAUTHENTICATED",表示请求未成功进行身份验证。

根据文档中的“创建授权凭据”,请检查您的身份验证过程,并确保提供了正确的身份验证凭据。您可能需要生成有效的OAuth 2访问令牌或使用适当的登录cookie进行身份验证。

错误代码"401"表示用户未被授权进行请求。为请求提供的授权凭据无效。请检查Authorization HTTP请求头的值。有关详细信息,请参阅文档

英文:

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

huangapple
  • 本文由 发表于 2023年7月31日 18:12:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定