首页 > 解决方案 > 调用 YouTube 插入 API 时缺少凭据

问题描述

我想使用 API 密钥而不是 OAuth 令牌从 YouTube v3 lib 调用插入 API。代码片段如下所示:

await google
.youtube("v3")
.videos.insert({
    key: "my-youtube-api-key",
    part: "id,snippet,status",
    notifySubscribers: false,
    requestBody: {
    snippet: {
        title: "Test video title",
        description: "Test video description",
    },
    status: {
        privacyStatus: "public",
    },
    },
    media: {
    body: fs.createReadStream(filePath),
    },
})
.catch((err) => {
    console.log("Upload to YouTube failed", err);
    return null;
});

但是,我遇到错误代码 401,消息是:

  code: 401,
  errors: [
    {
      message: 'Login Required.',
      domain: 'global',
      reason: 'required',
      location: 'Authorization',
      debugInfo: 'Authentication error: missing credentials.',
      locationType: 'header'
    }
  ]

我该如何解决这个问题?不支持 API 密钥吗?谢谢!

标签: node.jsgoogle-apiyoutube-apiyoutube-data-api

解决方案


根据文档Videos.insert,在端点上使用 API 密钥是不够的;您必须获得适当授权才能调用此端点:

授权

此请求需要至少具有以下范围之一的授权(阅读有关身份验证和授权的更多信息)。

范围

https://www.googleapis.com/auth/youtube.upload
https://www.googleapis.com/auth/youtube
https://www.googleapis.com/auth/youtubepartner
https://www.googleapis.com /auth/youtube.force-ssl


推荐阅读