Youtube Search:list getting PageInfo and nextPageToken null

两盒软妹~` 提交于 2019-12-06 12:57:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!