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

使用YouTube Data API更新自动生成字幕时遇403权限问题求助

问题

我尝试使用以下代码片段为频道内的视频插入/更新字幕:

let tokens = {
  access_token: 'ya29...',
  scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/youtubepartner https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtube.upload',
  token_type: 'Bearer',
  id_token: 'ey....',
  expiry_date: 1672523820523,
  user:{....}
}
async function upsertCaption(token, videoId, caption) {

  oauth2Client.setCredentials(token);

  return youtube.captions
    .list({
      auth: oauth2Client,
      part: ["snippet"],
      videoId: videoId
    })
    .then(response => {
      let track_id = _.get(response.data, "items[0].id", 0);
      console.log(`track id ${track_id}`);

      if (track_id == 0) {
        console.log("inserting new caption");
        return youtube.captions
          .insert({
            auth: oauth2Client,
            part: "snippet",
            sync: false,
            requestBody: {
              snippet: {
                language: "en",
                name: "my captions",
                videoId: videoId,
                isDraft: false
              }
            },
            media: {
              mimeType: "*/*",
              body: caption
            }
          })
          .then(response => {
            console.log(response.data);
            return response;
          });
      } else {
        console.log("updating caption");
        return youtube.captions
          .update({
            auth: oauth2Client,
            part: "snippet",
            sync: false,
            requestBody: {
              id: track_id,
              snippet: {
                language: "en",
                name: "my captions",
                videoId: videoId,
                isDraft: false
              }
            },
            media: {
              mimeType: "*/*",
              body: caption
            }
          })
          .then(response => {
            console.log(response.data);
            return response;
          });
      }
    });
}

所用令牌请求时包含以下scope:

scopes = [
  "profile",
  "https://www.googleapis.com/auth/youtube.upload",
  "https://www.googleapis.com/auth/youtube.force-ssl",
  "https://www.googleapis.com/auth/youtubepartner",
  "https://www.googleapis.com/auth/userinfo.profile"
];

代码逻辑在插入新字幕或更新自定义字幕时正常工作,但更新YouTube自动生成的字幕时返回如下错误:

{
  code: 403,
  errors: [
    {
      message: 'The permissions associated with the request are not sufficient to update the caption track. The request might not be properly authorized.',
      domain: 'youtube.caption',
      reason: 'forbidden',
      location: 'id',
      locationType: 'parameter'
    }
  ]
}

请问可能的问题是什么?

原因及解决方案

  • 自动生成字幕的权限限制:YouTube自动生成的字幕(标记为ASR类型)属于系统原生轨道,即使是频道所有者,也无法通过常规API权限直接更新这类轨道,现有youtube.upload、youtubepartner等权限不支持修改自动生成字幕。
  • 代码逻辑缺陷:当前代码直接取返回的第一条字幕轨道(items[0].id),如果这条轨道恰好是自动生成的,就会触发403权限错误。需要调整逻辑:
    1. 调用captions.list时,通过snippet.trackKind字段过滤轨道,只保留standard(自定义上传)类型的轨道。
    2. 若过滤后无结果,执行插入新字幕操作;若存在自定义轨道,再执行更新操作。
  • 替代方案:如果需要替换自动生成的字幕,正确方式是插入一条同语言的自定义字幕轨道,之后可将新轨道设为默认,或手动删除自动生成的轨道(删除需额外权限,但插入自定义轨道不受限制)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 06:32:12