gpt4 book ai didi

youtube - 使用Google Youtube API获取观看次数

转载 作者:行者123 更新时间:2023-12-03 05:57:18 25 4
gpt4 key购买 nike

我想获取一组视频的观看次数。以下是我代码的相关部分。

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

// Double checks the kind is video.
if (rId.getKind().equals("youtube#video")) {
Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().get("default");

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

YouTube.Videos.List list = youtube.videos().list("statistics");
list.setId(rId.getVideoId());
list.setKey("youtube.apikey");
Video v = list.execute().getItems().get(0);
System.out.println("The view count is: "+v.getStatistics().getViewCount());
System.out.println("\n-------------------------------------------------------------\n");
}

这会在“YouTube.Videos.Lists list = youtube.videos()。list(“statistics”);“行中出现以下错误。
error: method list in class YouTube.Videos cannot be applied to given types;

最佳答案

如果这是编译错误,则说明您所包含的库版本可能存在问题。我尝试了youtube API docs的示例代码,它对我有用。

我从示例中删除了一些额外的代码,以展示如何为单个视频检索观看次数:

    import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.services.samples.youtube.cmdline.Auth;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoListResponse;

import java.io.IOException;
import java.math.BigInteger;

public class GeolocationSearch {
public static void main(String[] args) {

try {
YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("APP_ID").build();

String apiKey = "API_KEY";
YouTube.Videos.List listVideosRequest = youtube.videos().list("statistics");
listVideosRequest.setId("lf_wVfwpfp8"); // add list of video IDs here
listVideosRequest.setKey(apiKey);
VideoListResponse listResponse = listVideosRequest.execute();

Video video = listResponse.getItems().get(0);

BigInteger viewCount = video.getStatistics().getViewCount();

System.out.println(" ViewCount: " + viewCount);
System.out.println("\n-------------------------------------------------------------\n");

} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}

}

关于youtube - 使用Google Youtube API获取观看次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33564057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com