PAYATHON 2026

YouTube Analytics API reports.query 全部频道 403

数据小陈

结论

同一个 OAuth token 调用 YouTube Data API v3 的 Channels.list 正常,但 YouTube Analytics API v2 的 reports.query 在所有频道、多个 Cloud 项目,甚至 Google 官方 APIs Explorer 中都返回通用 403 Forbidden(reason: forbidden),这时更可能是 Google 对账号、频道或 Analytics API 这条路径做了访问限制或权限判定。可以先排除 Apps Script、OAuth scope、Cloud 项目启用状态和配额这些方向。这类通用的 forbidden 一般不是改客户端代码就能解决的,需要做最小化复现,并带着证据联系 Google 官方支持。

另外,先确认一个基础问题:请求日期必须落在已经结束的有效区间内。原示例里的 2026-08-01 到 2026-08-31 如果还没到期,就换成过去的时间再测。

可能原因

  1. 账号或频道级限制:Brand Account、Workspace 账号、频道权限同步、地区/年龄/政策限制,或 Analytics API 对该账号的后端门控。如果官方 APIs Explorer 也返回 403,优先查这个方向。
  2. ids 与 token 上下文不匹配:channel==MINE 依赖授权账号当前对应的频道;Brand Account 场景容易落到错误频道。先用明确的 channel==<CHANNEL_ID>。
  3. OAuth 授权缓存或 scope 没有真正生效:改过 scope 后如果没有重新授权,旧 token 可能仍然缺少 yt-analytics.readonly。
  4. 日期范围或时区问题:未来日期、开始日期晚于结束日期、时区边界,都可能让请求异常。
  5. 项目或 API 配置问题:只有使用自己的项目时,才需要确认 YouTube Analytics API 已启用、OAuth consent screen 包含目标 scope、测试用户正确。

排查步骤

  1. 用官方 APIs Explorer 复现:打开 developers.google.com/youtube/analytics/reference/reports/query,在 Try it! 里用明确的频道 ID 和已经结束的日期查一次。如果也返回 403,就别再反复重建项目了,开始收集证据联系支持。
  2. 固定最小请求:只查 views,使用 channel==<CHANNEL_ID>,先不要用 channel==MINE:
GET https://youtubeanalytics.googleapis.com/v2/reports?ids=channel%3D%3D<CHANNEL_ID>&startDate=2026-04-01&endDate=2026-04-30&metrics=views
Authorization: Bearer <token>
  1. 确认 token 的实际 scope:在 Apps Script 中重新授权,确保 manifest 至少包含以下 scope;不要在生产日志里打印完整 token。
{
  "oauthScopes": [
    "https://www.googleapis.com/auth/yt-analytics.readonly",
    "https://www.googleapis.com/auth/youtube.readonly"
  ]
}
  1. 用 Apps Script 做一次可复现的诊断调用:
function queryYouTubeAnalytics() {
  const channelId = 'UCxxxxxxxxxxxxxxxxxxxxxx'; // 先用明确频道 ID
  const startDate = '2026-04-01'; // 必须是有效且已结束的日期
  const endDate = '2026-04-30';
  const metrics = 'views';

  const url = 'https://youtubeanalytics.googleapis.com/v2/reports'
    + '?ids=' + encodeURIComponent('channel==' + channelId)
    + '&startDate=' + encodeURIComponent(startDate)
    + '&endDate=' + encodeURIComponent(endDate)
    + '&metrics=' + encodeURIComponent(metrics);

  const res = UrlFetchApp.fetch(url, {
    method: 'get',
    headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },
    muteHttpExceptions: true
  });

  Logger.log(res.getResponseCode());
  Logger.log(res.getContentText());
}
  1. 交叉验证账号和频道:同一账号换频道、同一频道换 Google 账号、同一请求换 Cloud 项目。如果全部 403,重点转向账号或频道级限制;如果只有某些频道 403,记录这些频道的 Owner/Manager 关系、Brand Account 状态和创建时间。
  2. 检查日期与参数编码:确保 startDate、endDate 是 YYYY-MM-DD,且 startDate <= endDate;ids 中的 == 经过 URL 编码后应为 %3D%3D。
  3. 升级到官方支持:附上频道 ID、项目编号、OAuth client ID(不要附 token)、请求 URL、响应 JSON、时间戳、APIs Explorer 复现截图,以及已经排除的项目。说明 Data API v3 正常,Analytics API 在官方 Explorer 也返回 403。

注意事项

  • 403 Forbidden 与 403 quotaExceeded 不同;通用 forbidden 往往缺少可操作字段,不能靠重试解决。
  • 不要为了测试在生产环境关闭 OAuth 校验,也不要把 token 写进日志。
  • 如果官方 APIs Explorer 也复现,不要把时间继续花在 Apps Script 代码上;问题大概率不在代码层。
  • 若频道刚创建或刚迁移 Brand Account,权限同步可能有延迟,可以等待一段时间后用全新授权再测一次。

备注:内容仅供参考。