我使用 Predict 将基于 sklearn 的模型部署到 GCP AI 平台。使用 GCP AI Platform 控制台中的“TEST & USE”选项卡时,我能够成功测试模型。
但是,在使用终端进行测试时,出现错误:
curl -X POST -d '{"instances": [[12.85, 3.27, 2.58, 22.0, 106.0, 1.65, 0.6, 0.6, 0.96, 5.58, 0.87, 2.11, 570.0], [12.08, 1.13, 2.51, 24.0, 78.0, 2.0, 1.58, 0.4, 1.4, 2.2, 1.31, 2.72, 630.0], [12.0, 0.92, 2.0, 19.0, 86.0, 2.42, 2.26, 0.3, 1.43, 2.5, 1.38, 3.12, 278.0]]}' https://ml.googleapis.com/v1/projects/ml_deployment_tutorials/models/sklearn/versions/v1_0:predict\?access_token\=$(gcloud auth application-default print-access-token)
{
"error": {
"code": 403,
"message": "Permission denied on resource project ml_deployment_tutorials.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/ml_deployment_tutorials/apiui/credential"
}
]
}
]
}
另外,在 Postman 中进行测试时,我遇到了类似的错误:
{
"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"
}
}
但是,我确实已经在终端中登录了 gcloud sdk,因为运行时gcloud auth list
,我得到了:
Credentialed Accounts
ACTIVE ACCOUNT
ai-assets@appspot.gserviceaccount.com
***@***.com
***@gmail.com
* ml-deploy@datakademy.iam.gserviceaccount.com
服务帐户“ml-deploy@datakademy.iam.gserviceaccount.com”是我刚刚创建的,我下载了密钥文件并export GOOGLE_APPLICATION_CREDENTIALS="/home/george/Documents/datakademy-ml.json"
在终端中做了(创建服务帐户时,我还选择了角色“ML Engine Admin” ,还尝试了“所有者”)。这是成功的,因为当我这样做时gcloud auth application-default print-access-token
,我在下面得到了这个长字符串:
ya29.c.Kp0B7gcRXl4cyLCWnc-olIZ3Bh3Ak-YQO-t0fDOp-1nx6BWS5xGOv-4Qa6tS7RPYYfIwBw-mRgzPHoIlCM-eCt67wKQhYbfEvEafsB_V-*****
创建服务帐户后,我也在终端中激活了它:
gcloud auth activate-service-account ml-deploy@datakademy.iam.gserviceaccount.com --key-file=/home/george/Documents/datakademy-ml.json
Activated service account credentials for: [ml-deploy@datakademy.iam.gserviceaccount.com]