如何使用 Java API 访问 YouTube 观看次数

How to access YouTube view count with Java API

我发现 this answer 展示了如何通过 Java YouTube API 获取视频的观看次数,但很明显,在过去 7 年左右的时间里,有些事情肯定发生了变化,因为我想不通代码。

这一行

YouTube.Videos.List list = youtube.videos().list("statistics");

在 Eclipse 中产生错误,因为来自 class Videoslist 方法(至少在当前 API 版本中)有一个类型为 List<String> 不是 String...所以有人可以告诉我如何使用当前的 API 实现我的目标,因为上面链接的代码由于 API 的更改而无效?

编辑:这是我在 pom.xml 文件

中定义对 YouTube API 的依赖关系的方式
<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-youtube</artifactId>
  <version>v3-rev20201202-1.31.0</version>
</dependency>

感谢 stvar 的一点帮助,当然还有我在问题中链接到的旧问题,我想出了我需要做什么。这是工作代码:

import java.io.IOException;
import java.util.Arrays;

import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.services.youtube.*;
import com.google.api.services.youtube.model.Video;

public class Main {
    public static void main(String[] args) throws IOException
    {
        YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("youtube-view-count-test").build();
    
        YouTube.Videos.List list = youtube.videos().list(Arrays.asList("statistics"));
        list.setId(Arrays.asList("kffacxfA7G4"));
        String apiKey = "[redacted]";
        list.setKey(apiKey);            
        Video v = list.execute().getItems().get(0);
        System.out.println("The view count is: "+v.getStatistics().getViewCount());
    }
}

我的代码中使用的 Auth class 取自 here,Google 的 API 样本的一部分