问题
I am trying to get videos from YouTube using keyword. I am using YouTube API Java library.
I can get first 50 records successfully, but to fetch next records its required to set nextPageToken. But i am getting nextPageToken null
. Below is my code.
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();
}
回答1:
You are getting null because you didnt mention nextpagetoken or pageinfo fields, search result set is limited when you set fields explicitly.
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());
Output : Enter the search term
poker face
PAGE INFO: {"resultsPerPage":2,"totalResults":667891}
next page token :CAIQAA
prev page token:null
来源:https://stackoverflow.com/questions/33124788/youtube-searchlist-getting-pageinfo-and-nextpagetoken-null