youtube-api search response (java) -
using youtubeapi of java, made video search. worked using sample of following.
ttps://developers.google.com/youtube/v3/code_samples/java#search_by_keyword
the following data acquired.
{ "items": [ search resource ] }
and want "nextpagetoken" , "prevpagetoken". added following code.
searchresponse.getnextpagetoken();
but result "null";
what wrong?
reference: ttps://developers.google.com/youtube/v3/docs/search/list
this code.thanks.
/* * copyright (c) 2012 google inc. * * licensed under apache license, version 2.0 (the "license"); may not use file except * in compliance license. may obtain copy of license @ * * http://www.apache.org/licenses/license-2.0 * * unless required applicable law or agreed in writing, software distributed under license * distributed on "as is" basis, without warranties or conditions of kind, either express * or implied. see license specific language governing permissions , limitations under * license. */
package com.google.api.services.samples.youtube.cmdline.youtube_cmdline_search_sample; import com.google.api.client.googleapis.json.googlejsonresponseexception; import com.google.api.client.http.httprequest; import com.google.api.client.http.httprequestinitializer; import com.google.api.client.http.httptransport; import com.google.api.client.http.javanet.nethttptransport; import com.google.api.client.json.jsonfactory; import com.google.api.client.json.jackson2.jacksonfactory; import com.google.api.services.youtube.youtube; import com.google.api.services.youtube.youtube.search; import com.google.api.services.youtube.model.resourceid; import com.google.api.services.youtube.model.searchlistresponse; import com.google.api.services.youtube.model.searchresult; import com.google.api.services.youtube.model.thumbnail; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.iterator; import java.util.list; import java.util.properties; /** * prints list of videos based on search term. * * @author jeremy walker */ public class youtube_sample_2 { /** global instance properties filename. */ private static string properties_filename = "youtube.properties"; /** global instance of http transport. */ private static final httptransport http_transport = new nethttptransport(); /** global instance of json factory. */ private static final jsonfactory json_factory = new jacksonfactory(); /** global instance of max number of videos want returned (50 = upper limit per page). */ private static final long number_of_videos_returned = 25; /** global instance of youtube object make api requests. */ private static youtube youtube; /** * initializes youtube object search videos on youtube (youtube.search.list). program * prints names , thumbnails of each of videos (only first 50 videos). * * @param args command line args. */ public static void main(string[] args) { // read developer key youtube.properties properties properties = new properties(); try { inputstream in = search.class.getresourceasstream("/" + properties_filename); properties.load(in); } catch (ioexception e) { system.err.println("there error reading " + properties_filename + ": " + e.getcause() + " : " + e.getmessage()); system.exit(1); } try { /* * youtube object used make api requests. last argument required, * because don't need initialized when httprequest initialized, override * interface , provide no-op function. */ youtube = new youtube.builder(http_transport, json_factory, new httprequestinitializer() { public void initialize(httprequest request) throws ioexception {} }).setapplicationname("youtube-cmdline-search-sample").build(); // query term user. string queryterm = getinputquery(); youtube.search.list search = youtube.search().list("id,snippet"); // system.out.println(search); /* * important set developer key google developer console * non-authenticated requests (found under api access tab @ link: * code.google.com/apis/). practice , increased quota. */ string apikey = properties.getproperty("youtube.apikey"); search.setkey(apikey); search.setq(queryterm); /* * searching videos (not playlists or channels). if searching * more, add them string this: "video,playlist,channel". */ search.settype("video"); /* * method reduces info returned fields need , makes calls more * efficient. */ search.setfields("items(id/kind,id/videoid,snippet/title,snippet/thumbnails/default/url)"); search.setmaxresults(number_of_videos_returned); searchlistresponse searchresponse = search.execute(); system.out.println(searchresponse); // system.out.println(searchresponse.getnextpagetoken()); // system.out.println(searchresponse.getpageinfo()); list<searchresult> searchresultlist = searchresponse.getitems(); // system.out.println(searchresultlist); if (searchresultlist != null) { prettyprint(searchresultlist.iterator(), queryterm); } } catch (googlejsonresponseexception e) { system.err.println("there service error: " + e.getdetails().getcode() + " : " + e.getdetails().getmessage()); } catch (ioexception e) { system.err.println("there io error: " + e.getcause() + " : " + e.getmessage()); } catch (throwable t) { t.printstacktrace(); } } /* * returns query term (string) user via terminal. */ private static string getinputquery() throws ioexception { string inputquery = ""; system.out.print("please enter search term: "); bufferedreader breader = new bufferedreader(new inputstreamreader(system.in)); inputquery = breader.readline(); if (inputquery.length() < 1) { // if nothing entered, defaults "youtube developers live." inputquery = "youtube developers live"; } return inputquery; } /* * prints out searchresults in iterator. each printed line includes title, id, , * thumbnail. * * @param iteratorsearchresults iterator of searchresults print * * @param query search query (string) */ private static void prettyprint(iterator<searchresult> iteratorsearchresults, string query) { system.out.println("\n============================================================="); system.out.println( " first " + number_of_videos_returned + " videos search on \"" + query + "\"."); system.out.println("=============================================================\n"); if (!iteratorsearchresults.hasnext()) { system.out.println(" there aren't results query."); } while (iteratorsearchresults.hasnext()) { searchresult singlevideo = iteratorsearchresults.next(); resourceid rid = singlevideo.getid(); // double checks kind video. if (rid.getkind().equals("youtube#video")) { thumbnail thumbnail = (thumbnail) singlevideo.getsnippet().getthumbnails().get("default"); system.out.println(" video id" + rid.getvideoid()); system.out.println(" title: " + singlevideo.getsnippet().gettitle()); system.out.println(" thumbnail: " + thumbnail.geturl()); system.out.println("\n-------------------------------------------------------------\n"); } } } }
this propety file.
youtube.properties
youtube.apikey=aizasydlnvt0sjrc4hluwqbilxkgflwbsjsrvjq
user65415642,
i don't know youtube api of java have completed youtube api in app i.e iphone app(ios).
searchstring = [nsstring stringwithformat:@"https://www.googleapis.com/youtube/v3/search?part=snippet&contentdetails&q=%@&type=video&videosyndicated=true&key=%@&maxresults=%d", searchkey,youtube_api_key, results_per_call];
by calling url can response nextpage token
Comments
Post a Comment