问题描述
通过遵循教程.现在,我想通过HTTP请求对此模型进行预测.
I have deployed a model to AI Platform Predictions successfully by following this tutorial. Now, I want to make predictions with this model via HTTP requests.
按照@Ismail的建议,我遵循了方法:projects.predict 文档.
As suggested by @Ismail, I followed Method: projects.predict documentation.
我正在向 https://ml.googleapis.com/v1/projects/$ {PROJECT_ID}/models/$ {MODEL_NAME}:predict
发送POST请求,其正文如下:
I'm sending a POST request to https://ml.googleapis.com/v1/projects/${PROJECT_ID}/models/${MODEL_NAME}:predict
with the following body:
{
"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获得预测.
I sent the POST request using the RestMan chrome extension. I would like to be able to get a prediction with RestMan.
我的问题是:如何验证我的HTTP POST请求?我应该在请求的标题中发送一些信息吗?
My question is: how can I authenticate my HTTP POST request? Should I send some information in the request's header?
推荐答案
访问Google API时,需要在标头中添加访问令牌.使用CLI,您可以像这样生成它:
When you access to Google API, you need to add an access token in your header. With the CLI you can generate it like this:
gcloud auth print-access-token
当然,当前的凭据需要在AI平台预测服务上得到授权.
如果执行卷曲,则可以在linux(和云外壳)上执行
If you perform a curl, you can do this on linux (and 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,我不确定它是否能够为您生成访问令牌.解决方案是公开您的模型.不建议这样做,但是要在有限的时间内(仅用于测试),为什么不这样做!
If you use RestMan, I'm not sure it is capable to generate an access token for you. The solution is to make your model public. It's not recommended, but for a limited period of time, and for test only, why not!
为此,请为模型中的角色 roles/ml.modelUser
授予 allUsers
.
For this, grant allUsers
with the role roles/ml.modelUser
on your model.
编辑1
我没有找到如何在带有控制台的模型上执行此操作.我以前没做过,但是有一些更新...很奇怪.我通过gcloud CLI实现了这一点
I didn't find how to do this on a model with the console. I did t before but there were some updates... Very strange. I achieve this with the gcloud CLI
gcloud ai-platform models add-iam-policy-binding --region=us-central1 --member=allUsers --role=roles/ml.modelUser test
这篇关于如何使用HTTP请求对GCP AI平台预测进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!