GCP文本到语音API认证问题



我正在邮差中尝试上述api。下面是请求json:

{
"input":{
"text":"Flutter is awesome!"
},
"voice":{
"languageCode":"en-gb",
"name":"en-GB-Standard-A",
"ssmlGender":"FEMALE"
},
"audioConfig":{
"audioEncoding":"MP3"
}
}

对于auth,我在postman auth中选择Bearer,并首先在我的终端中执行以下命令来获取令牌:

gcloud auth application-default print-access-token

我将这个令牌粘贴到认证头中,我收到了以下响应:

{
"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 texttospeech.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/. If you are getting this error with curl or similar tools, you may need to specify 'X-Goog-User-Project' HTTP header for quota and billing purposes. For more information regarding 'X-Goog-User-Project' header, please check https://cloud.google.com/apis/docs/system-parameters.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "SERVICE_DISABLED",
"domain": "googleapis.com",
"metadata": {
"consumer": "projects/12345678910",
"service": "texttospeech.googleapis.com"
}
}
]
}
}

我是GCP的新手,不知道如何处理这个问题。对于额外的上下文,我试图使一个REST API调用,我发送文本,并得到一个base64编码的字符串包含音频回来。如有任何帮助,不胜感激。

这是令人困惑/复杂的,但错误是有帮助的:

您的应用程序已使用texttospeech.googleapis.com不支持的Google Cloud SDK或Google Cloud Shell的最终用户凭据进行了身份验证。

注意您可以在此链接中使用Google的api Explorer尝试此方法text.synthesize.

问题是gcloud是一个OAuth2应用程序,gcloud使用gcloud auth print-[access|identity]-tokengcloud auth application-default print-access-token发布的令牌是针对谷歌管理的项目(谷歌为gcloud提供)发布的,重要的是而不是你自己的一个项目。

Google希望为其用户提供gcloud,但不希望为其用户提供任意的API访问(免费)。因此出现了"不支持"。部分错误

解决方案(如上所述)是:

  1. 使用(或创建)您自己的Google Project
  2. 启用本项目的API
  3. 创建服务帐号和密钥
  4. gcloud auth activate-service-account提供服务帐户密钥
  5. gcloud auth print-access-token获取访问令牌以调用API

请参见以下链接查看步骤:

https://cloud.google.com/text-to-speech/docs/libraries

最新更新