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

在GitHub Actions中集成Google Cloud Python SDK遇认证问题求助

解决GCP翻译API在GitHub Actions中认证失败的问题

1. 检查服务账号权限

确保Workload Identity关联的GCP服务账号,已被授予Cloud Translation API User或Cloud Translation Admin角色。登录GCP IAM控制台,找到对应服务账号添加权限。

2. 验证环境变量传递

google-github-actions/auth@v2默认会设置GOOGLE_APPLICATION_CREDENTIALS环境变量指向生成的凭证文件,但可在Python脚本执行前添加命令确认变量存在:

echo $GOOGLE_APPLICATION_CREDENTIALS

若变量为空,可在auth步骤显式指定create_credentials_file: true,确保后续步骤继承该环境变量:

- uses: "google-github-actions/auth@v2"
  name: "Authenticate to Google Cloud"
  with:
    project_id: "project-12345"
    workload_identity_provider: "projects/12345/locations/global/workloadIdentityPools/github/providers/project"
    create_credentials_file: true

3. 显式指定凭证路径(可选)

若环境变量未生效,可在Python脚本中手动指定凭证文件路径初始化客户端:

from google.cloud import translate
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    "/home/runner/foo/bar/gha-creds-b8701ebd3eb851cb.json"  # 替换为实际凭证路径
)
client = translate.TranslationServiceClient(credentials=credentials)

4. 确认翻译API已启用

登录GCP控制台API库,检查Cloud Translation API是否已启用,未启用会导致请求失败。

5. 修正parent参数格式

parent参数需遵循projects/{PROJECT_ID}/locations/{LOCATION}格式(例如projects/project-12345/locations/global),而非单纯的项目ID。确认代码中projectId变量是否符合该格式。

内容的提问来源于stack exchange,提问作者Carlos Garces

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:22:37