【问题标题】:Google Cloud Text-to-Speech API - permission errorGoogle Cloud Text-to-Speech API - 权限错误
【发布时间】:2020-07-06 16:34:30
【问题描述】:

我正在尝试按照以下说明设置 Google Cloud Text-to-Speech API - https://cloud.google.com/text-to-speech/docs/quickstart 我已成功按照步骤 1-6 设置 Google SDK 并使用服务帐户凭据进行身份验证。 但是,当我尝试运行用于合成语音的示例 HTTP 请求时,我收到以下错误:

Cloud Text-to-Speech API 之前未在项目 usable-auth-library 中使用或已禁用。通过访问https://console.developers.google.com/apis/api/texttospeech.googleapis.com/overview?project=usable-auth-library 启用它,然后重试。

当点击错误消息中的链接时,它会转到以下页面:

API“texttospeech.googleapis.com”不存在或您无权访问它。

感谢您的帮助。

【问题讨论】:

  • 您的代码似乎正在尝试使用 usable-auth-library 项目而不是您的 actual 项目进行身份验证。我的猜测是这是由于凭据的使用方式。您是否尝试将 GOOGLE_APPLICATION_CREDENTIALS 环境变量设置为您的服务帐户 JSON 文件的位置?
  • 感谢您的回复。我已按照您的建议设置了 GOOGLE_APPLICATIONS_CREDENTIALS 但仍然出现相同的错误。 curl请求中的gcloud命令(curl -H“Authorization:Bearer”$(gcloud auth application-default print-access-token))是否需要更改?
  • 可能——如果你把application-default 拿出来,有帮助吗?如果您打算使用客户端库,则可能值得直接跳过,因为这样可以更轻松地确保您确实在使用服务帐户凭据。
  • 感谢您的回复。现在已解决此问题,方法是在设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量后重新运行 gcloud auth activate-service-account 步骤。
  • 确认我也按照建议取出了“应用程序默认”

标签: google-cloud-platform text-to-speech google-cloud-speech


【解决方案1】:

使用 API 密钥而不是 Google 在其文档中推荐的服务帐户密钥,我更容易集成到大多数 plarforms 中。

这些是获取 API 密钥所需的所有步骤

  1. Cloud Console 中创建一个项目(或使用现有项目)。
  2. 确保为您的项目启用billing
  3. 启用Text-to-Speech API
  4. 创建一个API key

您可能只需要最后一步(如果您按照您所说的正确执行了所有步骤)。

然后你就可以像这样使用 curl 命令了

Curl -H "X-Goog-Api-Key: PUT_YOUR_API_KEY_HERE" \
  -H "Content-Type: application/json; charset=utf-8" \
  --data "{
    'input':{
      'text':'Android is a mobile operating system developed by Google,
         based on the Linux kernel and designed primarily for
         touchscreen mobile devices such as smartphones and tablets.'
    },
    'voice':{
      'languageCode':'en-gb',
      'name':'en-GB-Standard-A',
      'ssmlGender':'FEMALE'
    },
    'audioConfig':{
      'audioEncoding':'MP3'
    }
  }" "https://texttospeech.googleapis.com/v1beta1/text:synthesize" > synthesize-text.txt

【讨论】:

  • 这为我解决了这个问题。
  • 这一步可以使用PHP的客户端库吗?
【解决方案2】:

如果这对任何人都有帮助,我在通过 Google 控制台将 API 添加到具有 JWT 凭据的现有 Google 服务帐户后遇到了此错误。

我点击了快速启动协议的链接,并且能够让它工作在下面的页面上,我点击了Enable the API

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

在下一页上,我单击了Create a project 下拉菜单并选择了一个现有项目。无需获取新凭据。

我正在使用 Go,然后能够使用 golang.org/x/oauth2/google 运行我的代码。

【讨论】:

    【解决方案3】:

    一旦你创建了一个 API 密钥,这里就是你的 curl 单行代码,它可以获取结果、解析它、格式化它并解码它。

    1. 在此处生成 API 密钥 https://console.cloud.google.com/apis/credentials?showWizardSurvey=true
    2. 安装jq JSON 解析器
    3. 运行:
    curl -H "X-Goog-Api-Key: APIKEYHERE" -H "Content-Type: application/json; charset=utf-8" --data "{ 'input':{ 'text':'I\'ve added the event to your calendar.' }, 'voice':{ 'languageCode':'en-gb', 'name':'en-GB-Standard-A', 'ssmlGender':'FEMALE' }, 'audioConfig':{ 'audioEncoding':'MP3' } }" "https://texttospeech.googleapis.com/v1/text:synthesize" | jq '.audioContent' | cut -d "\"" -f 2 > encodedOutput && base64 --decode encodedOutput > texttospeech.mp3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多