如何从其他频道youtube api node.js获取数据

发布于 2025-01-31 05:42:34 字数 1219 浏览 3 评论 0原文

我今天开始使用YouTube的API,因为我想制作一个可以显示YouTube频道数据的Discord机器人。因此,我在YouTube API站点上的指南遵循了Node.js指南,但遇到了问题。我不知道如何从与Google Developers不同的频道中获取数据(这是从解释中的渠道提取数据)。

function getChannel(auth) {
    var service = google.youtube('v3');
    service.channels.list({
      auth: auth,
      part: 'snippet,contentDetails,statistics',
      forUsername: 'GoogleDevelopers'
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No channel found.');
      } else {
        console.log('This channel\'s ID is %s. Its title is \'%s\', and ' +
                    'it has %s views.',
                    channels[0].id,
                    channels[0].snippet.title,
                    channels[0].statistics.viewCount);
      }
    });
  }

以上是他们使用的代码。我希望我可以将Google Developers更改为其他任何YouTube频道,它将从中返回数据,但是如果将其更改为例如Fireship,我会在下面获得错误。我搜索了他们的API参考,但我不太了解我在做什么错。

if (channels.length == 0) {
                   ^

TypeError: Cannot read properties of undefined (reading 'length')

我该怎么办来解决这个问题?

提前致谢!

I started using YouTube's API today since I want to make a Discord bot that can display a YouTube channels data. So, I went to the guides on the YouTube API site, followed the node.js guide, but ran into a problem. I do not know how I can get the data from a different channel than Google Developers (which is the channel their pulling data from in the explanation).

function getChannel(auth) {
    var service = google.youtube('v3');
    service.channels.list({
      auth: auth,
      part: 'snippet,contentDetails,statistics',
      forUsername: 'GoogleDevelopers'
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No channel found.');
      } else {
        console.log('This channel\'s ID is %s. Its title is \'%s\', and ' +
                    'it has %s views.',
                    channels[0].id,
                    channels[0].snippet.title,
                    channels[0].statistics.viewCount);
      }
    });
  }

Above is the code they use. I expected I could change Google Developers to any other YouTube channel and it would return the data from that, but if I change it to, for example Fireship, I get the error below. I searched their API reference, but I don't really understand what I'm doing wrong.

if (channels.length == 0) {
                   ^

TypeError: Cannot read properties of undefined (reading 'length')

What should I do to fix this issue?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2025-02-07 05:42:34

如果您检查文档的 channel.list.list.list.list 您会发现参数forusername

forusername参数指定youtube用户名,从而请求与该用户名关联的频道。

因此,如果您想为其他频道或其他用户找到它,请更改

forUsername: 'GoogleDevelopers'

If you check the docs for Channel.list you will find that the The parameter forUsername

The forUsername parameter specifies a YouTube username, thereby requesting the channel associated with that username.

so if you want to find it for a different channel or a different user then just change

forUsername: 'GoogleDevelopers'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文