You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GCP文本转语音Python客户端中设置正确的项目ID?

解决GCP Python文本转语音SDK项目ID错误问题

我在用GCP Python SDK的文本转语音示例,已经通过gcloud auth login登录并正确设置了项目,但运行示例时,SDK指向了错误的项目ID,报错如下:

raise exceptions.from_grpc_error(exc) from exc
google.api_core.exceptions.PermissionDenied: 403 This API method requires billing to be enabled. Please enable billing on project #841741138139 by visiting https://console.developers.google.com/billing/enable?project=xxxxxxx then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry. [links {
description: "Google developers console billing"
url: "https://console.developers.google.com/billing/enable?project=xxxx"

尝试设置GCP_PROJECT环境变量没用,想知道怎么在Python客户端里设置正确的项目ID。


方法1:客户端初始化时显式指定项目ID

直接在创建TextToSpeechClient实例时传入正确的项目ID参数:

from google.cloud import texttospeech

# 替换为你的正确项目ID
client = texttospeech.TextToSpeechClient(project="your-correct-project-id")

方法2:设置正确的环境变量

GCP Python SDK读取的是GOOGLE_CLOUD_PROJECT环境变量,而非GCP_PROJECT,按系统类型设置后再运行脚本:

  • Linux/macOS终端:
    export GOOGLE_CLOUD_PROJECT="your-correct-project-id"
    python your_script.py
    
  • Windows命令提示符:
    set GOOGLE_CLOUD_PROJECT=your-correct-project-id
    python your_script.py
    
  • Windows PowerShell:
    $env:GOOGLE_CLOUD_PROJECT="your-correct-project-id"
    python your_script.py
    

方法3:设置gcloud默认项目

确保本地gcloud工具的默认项目为正确值:

gcloud config set project "your-correct-project-id"

设置完成后重新运行脚本,SDK会自动读取该默认项目。

方法4:使用服务账号密钥文件

若上述方法无效,可下载正确项目的服务账号密钥JSON文件,通过以下方式指定:

方式A:环境变量指定密钥路径

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"

方式B:代码中直接加载密钥

from google.cloud import texttospeech
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    "/path/to/your/service-account-key.json"
)
client = texttospeech.TextToSpeechClient(credentials=credentials, project="your-correct-project-id")

内容的提问来源于stack exchange,提问作者Kintarō

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 02:27:36