【问题标题】:How to authenticate GCP AI Platform Predictions using HTTP requests如何使用 HTTP 请求对 GCP AI Platform Predictions 进行身份验证
【发布时间】:2021-01-26 14:09:04
【问题描述】:

我已按照此tutorial 成功将模型部署到 AI Platform Predictions。现在,我想通过 HTTP 请求使用此模型进行预测。

按照@Ismail 的建议,我遵循了Method: projects.predict 文档。

我正在向 https://ml.googleapis.com/v1/projects/${PROJECT_ID}/models/${MODEL_NAME}:predict 发送一个 POST 请求,正文如下:

{
  "instances": [
    [51.0, 17.0, 6.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0]
  ]
}

数字是模型的输入。

我收到以下回复:

{
    "error": {
        "code": 401,
        "message": "Request is missing required authentication credential. 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"
    }
}

我使用 RestMan chrome 扩展发送了 POST 请求。我希望能够使用 RestMan 进行预测。

我的问题是:如何验证我的 HTTP POST 请求?我应该在请求的标头中发送一些信息吗?

【问题讨论】:

  • 如果您需要更多详细信息,请确保使用您的用例更新您的问题。我将在下面相应地更新我的答案
  • @Ismail 刚刚更新了它。谢谢!
  • 您的 401 错误意味着您需要在运行请求之前进行身份验证 cloud.google.com/asset-inventory/docs/…
  • 你在哪里运行请求? Cloud Shell,您的本地终端或客户端库。 cloud.google.com/docs/authentication/getting-started
  • @Ismail 我想这是我必须在请求的 HEADER 中传递的一些令牌

标签: machine-learning google-cloud-platform httprequest google-cloud-iam google-ai-platform


【解决方案1】:

访问 Google API 时,需要在 header 中添加访问令牌。使用 CLI,您可以像这样生成它:

gcloud auth print-access-token

当然,当前的凭证需要在AI平台预测服务上进行授权。

如果您执行 curl,您可以在 linux(和 cloud shell)上执行此操作

curl -X POST -d @bodyfile.json -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  https://ml.googleapis.com/v1/projects/${PROJECT_ID}/models/${MODEL_NAME}:predic

如果您使用 RestMan,我不确定它是否能够为您生成访问令牌。解决方案是将您的模型公开。不推荐,但在有限的时间内,而且仅供测试,何乐而不为!

为此,请在您的模型上授予 allUsers 角色 roles/ml.modelUser


编辑 1

我没有找到如何在带有控制台的模型上执行此操作。我以前做过,但有一些更新......很奇怪。我通过 gcloud CLI 实现了这一目标

gcloud ai-platform models add-iam-policy-binding --region=us-central1 --member=allUsers --role=roles/ml.modelUser test

【讨论】:

  • 使用控制台,在我的模型页面中,我看不到授予角色的选项。如何使用控制台在我的模型上授予allUsers 角色roles/ml.modelUser? @guillaume
【解决方案2】:

通过关注Method: projects.predict 文档。

POST https://{endpoint}/v1/{name=projects/**}:predict

【讨论】:

  • 这只是请求的端点,没有说明如何处理身份验证。
猜你喜欢
  • 1970-01-01
  • 2023-03-10
  • 2017-11-11
  • 2023-03-31
  • 2014-09-06
  • 1970-01-01
  • 2016-04-28
  • 2020-07-20
  • 1970-01-01
相关资源
最近更新 更多