Gcloud 错误 401:请求的身份验证凭据无效

时间:2020-12-24 21:17:23

标签: gcloud speech-to-text google-speech-to-text-api

我正在处理一个需要访问 Gcloud 服务帐户的项目。但是,我一直遇到身份验证问题。这是我的命令提示符下的以下错误:

我的命令:

curl -s -H "Content-Type: application/json" \ 
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \ 
https://speech.googleapis.com/v1/speech:recognize \ 
-d @sync-request.json 

输出:

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. 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"
  }
}

目前,我已完成以下操作:我设置了环境变量“GOOGLE_APPLICATION_CREDENTIALS”,以保存我的服务帐户的 JSON 密钥文件的路径,我在密钥文件所在的目录中打开了我的 cmd,我运行了命令。还有什么我遗漏的吗?

我关注的文档来自https://cloud.google.com/docs/authentication/production#windows

1 个答案:

答案 0 :(得分:1)

几件事。

gcloud 不使用 ADC;设置 GOOGLE_APPLICATION_CREDENTIALS 不配置 gcloud

您应该(还没有尝试过)能够使用 gcloud auth activate-service-account ...gcloud auth print-access-token

或者您可以只使用常规(人工|非服务)帐户和 gcloud auth print-access-token

或者,您可以先使用 gcloud auth application-default login,然后使用 gcloud auth application-default print-access-token,但两者一起使用。

不要在标题中引用标记:

TOKEN="$(gcloud auth print-access-token)"                     # Either
TOKEN="$(gcloud auth application-default print-access-token)" # Or

curl \
--silent \
--header "Content-Type: application/json" \
--header "Authorization: Bearer ${TOKEN}" \
--data @sync-request.json \
https://speech.googleapis.com/v1/speech:recognize