调用Google视频智能API时出现PermissionDenied:403权限错误怎么办?
Google Video Intelligence API 403权限拒绝问题排查与解决
问题描述
运行GoogleCloudPlatform/python-docs-samples中的Video Intelligence API示例,执行命令:
python3 beta_snippets.py streaming-labels resources/cat.mp4
时触发google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission错误。
已尝试的方案:
- 使用
gcloud auth application-default login获取应用默认凭据 - 配置环境变量指定下载的JSON密钥文件
- 创建全新的服务账号
问题重现步骤:
- 激活虚拟环境:
source /home/[REDACTED]/bin/activate - 设置凭据环境变量:
export GOOGLE_APPLICATION_CREDENTIALS=/home/[REDACTED]/[REDACTED].json - 进入示例目录:
cd /home/[REDACTED]/python-docs-samples/videointelligence/samples/analyze - 执行示例命令:
python3 beta_snippets.py streaming-labels resources/cat.mp4
错误日志:
Traceback (most recent call last): File "/home/[REDACTED]/python-docs-samples/videointelligence/samples/analyze/beta_snippets.py", line 902, in detect_labels_streaming(args.path) File "/home/[REDACTED]/python-docs-samples/videointelligence/samples/analyze/beta_snippets.py", line 256, in detect_labels_streaming responses = client.streaming_annotate_video(requests, timeout=600) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/cloud/videointelligence_v1p3beta1/services/streaming_video_intelligence_service/client.py", line 750, in streaming_annotate_video response = rpc( ^^^^ File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/gapic_v1/method.py", line 131, in __call__ return wrapped_func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 293, in retry_wrapped_func return retry_target( ^^^^^^^^^^^^^ File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 153, in retry_target _retry_error_helper( File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/retry/retry_base.py", line 212, in _retry_error_helper raise final_exc from source_exc File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/retry/retry_unary.py", line 144, in retry_target result = target() ^^^^^^^^ File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/timeout.py", line 120, in func_with_timeout return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/[REDACTED]/[REDACTED]/lib/python3.11/site-packages/google/api_core/grpc_helpers.py", line 174, in error_remapped_callable raise exceptions.from_grpc_error(exc) from exc google.api_core.exceptions.PermissionDenied: 403 The caller does not have permission
解决方案
1. 确认服务账号权限
确保使用的服务账号已被授予Video Intelligence Viewer或Video Intelligence Editor角色(后者包含完整操作权限):
- 进入Google Cloud控制台IAM页面
- 找到对应服务账号,点击编辑权限
- 添加
roles/videointelligence.viewer或roles/videointelligence.editor角色后保存
2. 验证API是否启用
确认Video Intelligence API在当前项目中已启用:
- 进入Google Cloud控制台API库
- 搜索"Video Intelligence API",检查状态是否为"已启用",未启用则点击启用
3. 检查凭据配置有效性
- 确认
GOOGLE_APPLICATION_CREDENTIALS环境变量指向的JSON文件路径正确、文件无损坏 - 执行以下命令验证凭据:
gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS gcloud auth print-access-token
若能正常输出令牌,说明凭据配置无误
4. 更新客户端库版本
确保虚拟环境中安装的Google Cloud客户端库为最新版,执行:
pip install --upgrade google-cloud-videointelligence
内容的提问来源于stack exchange,提问作者user23506502
相关产品推荐
相关产品推荐

