YouTube 数据 API - concurrentViewers 抛出错误
YouTube Data API - concurrentViewers throwing an error
我试图从实时视频中获取并发观看者,但每当我尝试将值存储在变量中时,我总是收到错误消息。
代码片段如下。错误在 var videoConcureentViewrs...
行
var part = 'snippet,statistics';
var params = {'id': videoID};
var response = YouTube.Videos.list(part, params);
var video = response.items[0];
var videoViewCount = video.statistics.viewCount;
var videoConcurrentViewrs = video.liveStreamingDetails.concurrentViewers;
修改点:
- 在这种情况下,请在部分的值中包含
liveStreamingDetails
。
- 并且,在
YouTube.Videos.list
,参数的 part
可以作为数组给出。
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
var part = ['snippet', 'statistics', 'liveStreamingDetails']; // Modified
var params = {'id': videoID};
var response = YouTube.Videos.list(part, params);
var video = response.items[0];
var videoViewCount = video.statistics.viewCount;
var videoConcurrentViewrs = video.liveStreamingDetails.concurrentViewers;
console.log(videoConcurrentViewrs) // Added
注:
如果你的videoID
不是直播视频,concurrentViewers
就是undefined
。请注意这一点。
liveStreamingDetails
: The liveStreamingDetails object contains metadata about a live video broadcast. The object will only be present in a video resource if the video is an upcoming, live, or completed live broadcast.
参考:
我试图从实时视频中获取并发观看者,但每当我尝试将值存储在变量中时,我总是收到错误消息。
代码片段如下。错误在 var videoConcureentViewrs...
var part = 'snippet,statistics';
var params = {'id': videoID};
var response = YouTube.Videos.list(part, params);
var video = response.items[0];
var videoViewCount = video.statistics.viewCount;
var videoConcurrentViewrs = video.liveStreamingDetails.concurrentViewers;
修改点:
- 在这种情况下,请在部分的值中包含
liveStreamingDetails
。 - 并且,在
YouTube.Videos.list
,参数的part
可以作为数组给出。
当以上几点反映到你的脚本中,就会变成下面这样。
修改后的脚本:
var part = ['snippet', 'statistics', 'liveStreamingDetails']; // Modified
var params = {'id': videoID};
var response = YouTube.Videos.list(part, params);
var video = response.items[0];
var videoViewCount = video.statistics.viewCount;
var videoConcurrentViewrs = video.liveStreamingDetails.concurrentViewers;
console.log(videoConcurrentViewrs) // Added
注:
如果你的
videoID
不是直播视频,concurrentViewers
就是undefined
。请注意这一点。liveStreamingDetails
: The liveStreamingDetails object contains metadata about a live video broadcast. The object will only be present in a video resource if the video is an upcoming, live, or completed live broadcast.