请求包含无效的身份验证凭据。 AutoML API中预期的OAuth 2访问令牌错误

时间:2019-04-24 06:25:03

标签: api oauth google-api google-cloud-platform

我遵循了Google Cloud AutoML API快速入门,该指南通过使用

请求api。
curl -X POST \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  -H "Content-Type: application/json" \
  https://automl.googleapis.com/v1beta1/projects/kintsugi-415310213303/locations/us-central1/models/TCN1592956918625708154:predict \
  -d '{
        "payload" : {
          "textSnippet": {
               "content": "YOUR TEXT HERE",
                "mime_type": "text/plain"
           },
        }
      }'

并跟随link,但出现此错误

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. Se
e https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

我参考了Request had invalid authentication credentials. Expected OAuth 2 access token error in cloud speech api,并尝试使用应用程序默认登录名创建另一个访问令牌,并出现以下错误:

{
  "error": {
    "code": 403,
    "message": "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by
 the automl.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how
 to use them in your application, see https://cloud.google.com/docs/authentication/.",
    "status": "PERMISSION_DENIED"
  }
}

我还启用了类似于以下的权限:https://github.com/googleapis/google-cloud-php/issues/1722(AutoML Admin,AutoML Service Agent,AutoML编辑器)

我是该项目的所有者/管理员。令牌似乎已过期,我可能需要实现此服务器端。

我现在该怎么办?

谢谢!

1 个答案:

答案 0 :(得分:0)

意识到我指向了错误的json文件,重新生成了密钥(https://cloud.google.com/video-intelligence/docs/common/auth),并将路径直接拖放到终端中并能够生成本地结果。

let wordsArray = ["why", "would", "you", "pay", "for", "a", "phone", "?","-"];
var regex = /^[a-zA-Z0-9]*$/;
let grouped = wordsArray.reduce(function(acc, curr) {
  
  let isSpecial = regex.test(curr);

  if (!isSpecial) {
    acc.special.push(curr)
  } else if (acc.hasOwnProperty(curr.charAt(0))) {
    acc[curr.charAt(0)].push(curr)
  } else {
    acc[curr.charAt(0)] = [curr]

  }
  return acc;

}, {
  special: []
})

console.log(grouped)