Youtube Data API v3 java搜索与分页问题

问题描述 投票:1回答:2

我正在使用youtube数据api v3,但我有一个奇怪的问题。我确实理解,对于分页,我需要在发送后续请求时使用响应中的nextPageTokem,但我的问题是,我没有在响应中获得nextPageToken。我的代码如下。

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


  String queryTerm = "<my movie>";

  YouTube.Search.List search = youtube.search().list("id,snippet");


  String apiKey = properties.getProperty("youtube.apikey");
  search.setQ(queryTerm);
  search.setVideoDuration("long");

  search.setType("video");
  search.setFields("items(*)");
  SearchListResponse searchResponse = search.execute();
      System.out.println(searchResponse.toPrettyString());
      System.out.println(searchResponse.getNextPageToken());

  List<SearchResult> searchResultList = searchResponse.getItems();


  if (searchResultList != null) {
      System.out.println(searchResponse.getPageInfo());
      prettyPrint(searchResultList.iterator(), queryTerm);
  }

我错过了什么?我是否需要设置一些东西来获取响应中的标题?

提前感谢您的回答

java youtube-api google-api-java-client
2个回答
3
投票

这是因为您将字段设置为仅返回“项目”。如果您只想返回item和nextpageToken,可以将其设置为“items,nextPageToken”


0
投票
// Recursive function to print an entire feed.
 public static void printEntireVideoFeed(YouTubeService service, 
VideoFeed videoFeed, boolean detailed) throws MalformedURLException, 
IOException, ServiceException {
 do {
     printVideoFeed(videoFeed, detailed);
     if(videoFeed.getNextLink() != null) {
     videoFeed = service.getFeed(new URL(videoFeed.getNextLink().getHref()), 
     VideoFeed.class);
     }
 else {
        videoFeed = null;
       }  
    }
while(videoFeed != null);
 }

//使用递归函数的示例。打印搜索词“puppy”的所有结果。

YouTubeQuery query = 
new YouTubeQuery(new URL("http://gdata.youtube.com/feeds/api/videos"));
query.setFullTextQuery("puppy");
VideoFeed videoFeed = service.query(query, VideoFeed.class);
printEntireVideoFeed(service, videoFeed, false);
© www.soinside.com 2019 - 2024. All rights reserved.