在 YouTube V3 API for java 中获取观看次数

如何通过 Java API 提供某个 YouTube 视频的 videoID 来获取该视频的观看次数信息?

How can I get the information of how many views a certain video of YouTube has by providing the videoID of it using Java API?

例如,在他们提供的代码示例中,它们打印不同的属性.如何添加打印查看次数?

for example in the code sample that their provide they print different properties. How can I add printing the view Count?

Java 代码:

        SearchResult singleVideo = iteratorSearchResults.next();
        ResourceId rId = singleVideo.getId();

        // Confirm that the result represents a video. Otherwise, the
        // item will not contain a video ID.
        if (rId.getKind().equals("youtube#video")) {
            Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault();

            System.out.println(" Video Id" + rId.getVideoId());
            System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
            System.out.println(" Thumbnail: " + thumbnail.getUrl());
            System.out.println("
-------------------------------------------------------------
");

这里有一个与我非常相似的问题,也没有答案:
在 Search.List 中获取 ViewCount - Youtube Data API v3

here is a very similar question to mine, also unanswered:
Get ViewCount in Search.List - Youtube Data API v3

30/9 更新:根据 Youtube API tutorial,视频资源可能包含以下部分:

update 30/9: According to Youtube API tutorial the video resource may contain the following parts:

  • 片段
  • 内容详情
  • 文件详情
  • 播放器
  • 处理详情
  • 录音详情
  • 统计数据
  • 状态
  • 建议
  • 主题详情

但在 SearchResult 对象只公开所有的 getSnippet()

but in the SearchResult object only expose getSnippet() out of all of them

除了 部分m/youtube/v3/youtube.search.list?part=statistics&q=like%20a%20virgin&_h=2&"google 提供的 rel="nofollow noreferrer">API Explorer.

I also couldn't use a different part except the 'snippet' in the API Explorer that google provides.

那么 API V3 可能还不支持所有这些统计属性??

So maybe all these statistics properties are not supported yet by API V3??

推荐答案

好的,我知道了,代码是:

OK I got it the code to do it is:

YouTube.Videos.List list = youtube.videos().list("statistics");
list.setId("kffacxfA7G4");
list.setKey("your private API KEY");            
Video v = list.execute().getItems().get(0);
        System.out.println("The view count is: "+v.getStatistics().getViewCount());

顺便说一句,这个视频的观看次数超过了十亿 - 贾斯汀比伯规则!(-;

This video BTW exceed the billion views - Justim Bieber rules! (-;

相关文章