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

使用委托服务账户登录YouTube品牌账户时的权限错误

问题

我尝试通过委托服务账户凭证以编程方式获取YouTube品牌账户的报告数据,但遇到权限问题。目前不提供onBehalfOfContentOwner参数时,API请求能成功返回账户正确数据,说明服务账户委托配置没问题。但加上该参数请求内容所有者数据时,返回403权限错误。

我已经验证过委托邮箱有权限通过API查询品牌账户数据——在YouTube文档的「Try it yourself」板块和OAuth playground里都能成功请求到内容所有者/品牌账户的数据。想问下有没有办法生成对应目标品牌账户的令牌?或者我还遗漏了什么配置?

代码示例

from google.oauth2 import service_account
import googleapiclient.discovery

SCOPES = [
    'https://www.googleapis.com/auth/youtube',  
    'https://www.googleapis.com/auth/yt-analytics-monetary.readonly',
    'https://www.googleapis.com/auth/yt-analytics.readonly'
]

credentials = service_account.Credentials.from_service_account_info(
    service_account_cred, 
    scopes=SCOPES,
    subject='myemail@mycompany.com'
)

credentials.refresh(
    google.auth.transport.requests.Request())

youtube_reporting = googleapiclient.discovery.build(
    'youtubereporting', 'v1', credentials=credentials)

print(youtube_reporting.reportTypes().list(onBehalfOfContentOwner='mycontentownerid').execute())

错误信息

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://youtubereporting.googleapis.com/v1/reportTypes?onBehalfOfContentOwner=mycontentownerid&alt=json returned "The caller does not have permission". Details: "The caller does not have permission">

解决建议

  • 给服务账户添加内容所有者权限:服务账户必须被添加为目标内容所有者的授权用户,通过YouTube Studio的内容所有者设置面板操作。不能只依赖委托邮箱的权限,服务账户本身无授权的话,即便委托邮箱有权限也无法访问。
  • 补充YouTube Reporting的权限Scope:当前SCOPES列表缺少https://www.googleapis.com/auth/youtubereporting,这个是调用YouTube Reporting API的必需权限,必须添加进去。
  • 确认内容所有者ID准确:检查mycontentownerid是否为目标品牌账户的有效ID,可在OAuth playground用委托邮箱调用YouTube Data API的contentOwners.list接口获取正确ID。
  • 核对域委派的Scope范围:在Google Workspace的域委派设置里,确保服务账户的委派范围包含所有需要的API权限,尤其是新增的youtubereporting权限,避免因Scope遗漏导致权限不足。

内容的提问来源于stack exchange,提问作者Greg Gascon

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 15:35:27