如何从其他频道youtube api node.js获取数据
我今天开始使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您检查文档的 channel.list.list.list.list 您会发现参数
forusername
因此,如果您想为其他频道或其他用户找到它,请更改
If you check the docs for Channel.list you will find that the The parameter
forUsername
so if you want to find it for a different channel or a different user then just change