调用 YouTube 插入时缺少凭据 API

Missing credentials while calling YouTube insert API

我想使用 API 密钥而不是 OAuth 令牌从 YouTube v3 库调用插入 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 键吗?谢谢!

根据the docs, it's insufficient to use an API key on Videos.insert端点;您必须获得适当授权才能调用此端点:

Authorization

This request requires authorization with at least one of the following scopes (read more about authentication and authorization).

Scope

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