Java 从Youtube官方频道获取视频
Youtube上有一些官方频道,如音乐、游戏、新闻等。我正在尝试获取这些频道的信息,好像这是一个普通频道,但它给我发送了无效的响应。这可能是因为频道视频不归频道所有,即使频道将其显示为“上传视频”。这是我的代码,用于“正常”频道:(Java 从Youtube官方频道获取视频,java,youtube-api,Java,Youtube Api,Youtube上有一些官方频道,如音乐、游戏、新闻等。我正在尝试获取这些频道的信息,好像这是一个普通频道,但它给我发送了无效的响应。这可能是因为频道视频不归频道所有,即使频道将其显示为“上传视频”。这是我的代码,用于“正常”频道:(stringid表示每个视频的id以逗号分隔) 此方法提供频道视频的视频,并与“正常”频道一起使用 public static ArrayList<VideoYoutube> getVideos (String ids) throws IOExceptio
stringid
表示每个视频的id以逗号分隔)
此方法提供频道视频的视频,并与“正常”频道一起使用
public static ArrayList<VideoYoutube> getVideos (String ids) throws IOException
{
try
{
YouTube.Videos.List videos = youtube.videos().list("id,snippet,statistics");
videos.setKey(API_KEY);
//Setting all the video ids we want to get back
videos.setId(ids);
videos.setMaxResults(RETURN_NUMBER);
//Setting all the fields we want to be called from the api. Its such an
videos.setFields("items(id,snippet/publishedAt,snippet/title,snippet/description," +
"snippet/thumbnails/default/url,snippet/thumbnails/medium/url,snippet/thumbnails/high/url," +
"snippet/channelId,snippet/channelTitle,statistics/viewCount,statistics/likeCount,statistics/dislikeCount," +
"statistics/favoriteCount,statistics/commentCount)");
VideoListResponse videoResponse = videos.execute();
List<Video> videoResultList = videoResponse.getItems();
}
public static String getChannelVideosIds(String channelId) throws IOException
{
YouTube.Search.List search = youtube.search().list("id");
//Define the search key
search.setKey(API_KEY);
search.setChannelId(channelId);
//Give the type (video, playlist or channel)
search.setType("video");
//Giving the params we want to be returned comma separated(for example if we had choosed the snipped before, we could choose snipped fields like snipped/publishedAt)
search.setFields("items(id)");
//set the max return number
search.setMaxResults(RETURN_NUMBER);
//Execute the search query
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
//check if the result is null
return retrieveIds(searchResultList);
}
publicstaticarraylistgetvideos(stringid)抛出IOException
{
尝试
{
YouTube.Videos.List Videos=YouTube.Videos();
视频。设置键(API_键);
//设置我们想要返回的所有视频ID
视频.setId(ids);
videos.setMaxResults(返回\u编号);
//设置我们希望从api调用的所有字段
设置字段(“项目(id、代码段/发布数据、代码段/标题、代码段/说明,”+
snippet/thumbnails/default/url、snippet/thumbnails/medium/url、snippet/thumbnails/high/url+
snippet/channelId、snippet/channelTitle、statistics/viewCount、statistics/likeCount、statistics/dislikeCount、+
“统计/收藏计数、统计/评论计数”);
VideoListResponse videoResponse=videos.execute();
List videoResultList=videoResponse.getItems();
}
这是一种从我想要的频道检索视频ID的方法。它为这个频道提供null,但它使用的是“正常”频道
public static ArrayList<VideoYoutube> getVideos (String ids) throws IOException
{
try
{
YouTube.Videos.List videos = youtube.videos().list("id,snippet,statistics");
videos.setKey(API_KEY);
//Setting all the video ids we want to get back
videos.setId(ids);
videos.setMaxResults(RETURN_NUMBER);
//Setting all the fields we want to be called from the api. Its such an
videos.setFields("items(id,snippet/publishedAt,snippet/title,snippet/description," +
"snippet/thumbnails/default/url,snippet/thumbnails/medium/url,snippet/thumbnails/high/url," +
"snippet/channelId,snippet/channelTitle,statistics/viewCount,statistics/likeCount,statistics/dislikeCount," +
"statistics/favoriteCount,statistics/commentCount)");
VideoListResponse videoResponse = videos.execute();
List<Video> videoResultList = videoResponse.getItems();
}
public static String getChannelVideosIds(String channelId) throws IOException
{
YouTube.Search.List search = youtube.search().list("id");
//Define the search key
search.setKey(API_KEY);
search.setChannelId(channelId);
//Give the type (video, playlist or channel)
search.setType("video");
//Giving the params we want to be returned comma separated(for example if we had choosed the snipped before, we could choose snipped fields like snipped/publishedAt)
search.setFields("items(id)");
//set the max return number
search.setMaxResults(RETURN_NUMBER);
//Execute the search query
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
//check if the result is null
return retrieveIds(searchResultList);
}
公共静态字符串GetChannelVideoSID(字符串channelId)引发IOException
{
YouTube.Search.List Search=YouTube.Search().List(“id”);
//定义搜索键
search.setKey(API_KEY);
search.setChannelId(channelId);
//提供类型(视频、播放列表或频道)
search.setType(“视频”);
//给我们想要返回的参数以逗号分隔(例如,如果我们以前选择了剪切字段,我们可以选择剪切字段,如snipped/publishedAt)
search.setFields(“items(id)”);
//设置最大返回编号
search.setMaxResults(返回\u编号);
//执行搜索查询
SearchListResponse searchResponse=search.execute();
List searchResultList=searchResponse.getItems();
//检查结果是否为空
返回RetrieveId(searchResultList);
}