我正试图使用关键字从YouTube获得视频。我正在使用YouTube API Java库。我可以获得前50条记录成功,但获取下一个记录需要设置nextPageToken。但我得到nextPageToken null
。下面是我的代码。
YouTube.Search.List search = youtube.search().list("id,snippet");
search.setOauthToken(credential.getAccessToken());
search.setMaxResults(50L);
search.setQ(keywords); // keywords is string type and input by user.
search.setType("video");
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
VideoList returnList = new VideoList();
do {
search.setKey("MYKEY");
SearchListResponse searchResponse = search.execute();
// This get 50 items proper.
List<SearchResult> searchResultList = searchResponse.getItems();
// Why PageInfo is null?
PageInfo pageInfo = searchResponse.getPageInfo();
// int totalResults = pageInfo.getTotalResults();
// int pageSize = pageInfo.getResultsPerPage();
if(searchResponse != null){
// i use searchResultList and update my database for search.
....
....
// PageToken also null.
String pageToken = searchResponse.getNextPageToken();
search.setPageToken(pageToken);
}
else{
break;
}
} while (true);
}catch(Exception ex){
ex.printStackTrace();
}
你得到空,因为你没有提到nextpagetoken或pageinfo字段,当显式设置字段时,搜索结果集受到限制。
search.setFields("items(kind,id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url),nextPageToken,pageInfo,prevPageToken");
System.out.println("PAGE INFO: " +response.getPageInfo());
System.out.println("next page token :" +response.getNextPageToken());
System.out.println("prev page token:"+ response.getPrevPageToken());
输出:输入搜索词
扑克脸
PAGE INFO: {"resultsPerPage":2,"totalResults":667891}
下一页令牌:CAIQAA