android - Get all videos of a YouTube Playlist in Java -
context i'm working android studio (java). want obtain videos of given playlist (or 50, other after).
problem see people using url https://www.googleapis.com/youtube/v3/playlistitems?part=snippet&maxresults=50&playlistid=pliewzfngb4fvrrztonlvemj6db2nmzg2m&key=aizasyc2_yrcte9916fsma0_krnef43gblzz8m0
don't know how implement in java. follow tuto , got way information totally different :
youtube.search.list query; query = youtube.search().list("id,snippet"); query.setkey(my_api_key); query.setmaxresults((long)20); query.settype("video"); query.setfields("items(id/videoid,snippet/title,snippet/description,snippet/thumbnails/default/url)");
and don't understand how else search. of documentation in english only...
edit ok, continued try, think got near solution, got error.
private youtube youtube; private youtube.playlistitems.list playlistitemrequest; private string playlist_id = "pliewzfngb4fvrrztonlvemj6db2nmzg2m"; public static final string key = "aizasyc2_yrcte9916fsma0_krnef43gblzz8m0"; // constructor public youtubeconnector(context context) { youtube = new youtube.builder(new nethttptransport(), new jacksonfactory(), new httprequestinitializer() { @override public void initialize(httprequest hr) throws ioexception {} }).setapplicationname(context.getstring(r.string.app_name)).build(); } public list<videoitem> result() { list<playlistitem> playlistitemlist = new arraylist<playlistitem>(); try { /* here must problem ! */ playlistitemrequest = youtube.playlistitems().list("snippet"); playlistitemrequest.setplaylistid(playlist_id); playlistitemrequest.setfields("items(id/videoid,snippet/title,snippet/description,snippet/thumbnails/default/url),nextpagetoken,pageinfo"); playlistitemrequest.setkey(key); string nexttoken = ""; { playlistitemrequest.setpagetoken(nexttoken); playlistitemlistresponse playlistitemresult = playlistitemrequest.execute(); playlistitemlist.addall(playlistitemresult.getitems()); nexttoken = playlistitemresult.getnextpagetoken(); } while (nexttoken != null); }catch(ioexception e) { log.d("yc", "could not initialize: "+e); } //[...] }
here error got : { "code" : 400, "errors" : [ { "domain" : "global", "location" : "fields", "locationtype" : "parameter", "message" : "invalid field selection videoid", "reason" : "invalidparameter" } ], "message" : "invalid field selection videoid" }
edit 2 : martijn woudstra. correct line :
playlistitemrequest = youtube.playlistitems().list("snippet,contentdetails"); //[...] playlistitemrequest.setfields("items(snippet/title,snippet/description,snippet/thumbnails/default/url,contentdetails/videoid),nextpagetoken,pageinfo"); //[...] videoitem.setid(item.getcontentdetails().getvideoid());
i know old question important identify resources using understand how proper information. there many resources in youtube api v3, use search
, video
, playlist
, playlistitems
.
according documentation following json structure shows format of playlistitems
resource:
{ "kind": "youtube#playlistitem", "etag": etag, "id": string, "snippet": { "publishedat": datetime, "channelid": string, "title": string, "description": string, "thumbnails": { (key): { "url": string, "width": unsigned integer, "height": unsigned integer } }, "channeltitle": string, "playlistid": string, "position": unsigned integer, "resourceid": { "kind": string, "videoid": string, } }, "contentdetails": { "videoid": string, "startat": string, "endat": string, "note": string, "videopublishedat": datetime }, "status": { "privacystatus": string } }
from structure, may suppose there 3 ways videoid
. first important know how going define parts , fields of resource.
to define parts use code:
youtube.playlistitems.list list = youtube.playlistitems().list("snippet");
in previous line, "snippet"
identifies property contains numerous fields (or child properties), including title
, description
, position
, , resourceid
, when set "snippet"
api's response contain of child properties.
now, can limit previous properties if define fields. example, in code:
list.setfields("items(id/videoid,snippet/title,snippet/description," + "snippet/thumbnails/default/url)");
if call list.execute()
, show error because didn't define id
in parts properties. also, according json structure, id
string , not contains videoid
child property. ah!, can extract videoid
resourceid
? -well, answers yes/no. -why so? come on teo, json structure shows clearly. -yes, can see that, documentation says:
if
snippet.resourceid.kind
property's valueyoutube#video
, property present , value contain id youtube uses uniquely identify video in playlist.
this means sometimes may not available. -then, how can videoid
? -well, can add id
or contentdetails
parts resources. if add id
defines fields this:
youtube.playlistitems.list list = youtube.playlistitems().list("id,snippet"); list.setfields("items(id,snippet/title,snippet/description," + "snippet/thumbnails/default/url)");
if add contentdetails
defines fields this:
youtube.playlistitems.list list = youtube.playlistitems() .list("snippet,contentdetails"); list.setfields("items(contentdetails/videoid,snippet/title,snippet/description," + "snippet/thumbnails/default/url)");
i hope helps guys.
Comments
Post a Comment