我使用Eclipse Kepler和Youtube Data Api v3。
我正在尝试使用以下方式获取频道的订阅者数量:
Channel channel = new Channel();
channel.getStatistics().getSubscriberCount();
但是获取此信息似乎是错误的方式。
有人可以告诉我如何获得频道订阅者数量吗?
答案 0 :(得分:1)
确保您使用的是正确的客户端库。此示例基于YouTube Data API v3
<强>的pom.xml 强>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>v3-rev125-1.19.1</version>
</dependency>
<强> SubscriberCount.java 强>
HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
}
};
YouTube youTube = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), httpRequestInitializer)
.setApplicationName("<Your-Application-Name>")
.build();
YouTube.Channels.List search = youTube.channels().list("statistics");
search.setForUsername("codeherenow");
search.setKey("<Insert-API-Key>");
ChannelListResponse response = search.execute();
List<Channel> channels = response.getItems();
for (Channel channel : channels) {
System.out.println(channel.getStatistics().getSubscriberCount());
}