Ben
Ben

Reputation: 9001

How to receive a JSON list of a channel's videos on YouTube?

What I'm trying to do

I'm trying to display a list of videos that have been uploaded to a channel.

Where I am so far

I understand that this is the correct URL for a user's 'uploads' in JSON format:

http://gdata.youtube.com/feeds/api/users/%USERNAME%/uploads?alt=json&v=2

where the USERNAME is the channel name rather than the string of letters/digits that correspond to the user's channel (eg, MyChannel rather than hIANSUBhkj_baisu128).

However, when testing this on my username (BenPearlMagic), I receive a hugely long JSON list that contains plenty of data that seems irrelevant.

My code is this:

$.getJSON('http://gdata.youtube.com/feeds/api/users/benpearlmagic/uploads?alt=json', function(data) {
    console.log(data);
    for(var i=0; i<data.data.items.length; i++) {
       console.log(data.data.items[i].title); // title
       console.log(data.data.items[i].description); // description
    }
});

...but I can't get it to work.

Upvotes: 2

Views: 8847

Answers (3)

Mario Gonzales Flores
Mario Gonzales Flores

Reputation: 705

version 2 is already deprecated soon, google only give you support until April 2015 for the new version v3 uses this, before generate your api developer key and enable YouTube Data API v3:

https://developers.google.com/youtube/v3/getting-started

var videosURL = "https://www.googleapis.com/youtube/v3/playlistItems?playlistId={myPlaylistID}&key={myAPIKey}&fields=items&part=snippet&maxResults=6&callback=?";


$.getJSON(videosURL, function(data) {
$.each(data.items, function(i,val) {
console.log(val.snippet.title);
console.log(val.snippet.resourceId.videoId);   
 });
});

how get your playlistid?

https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername={myChannelName}&key={myAPIKey}

favorites, likes or uploads

Upvotes: 7

user2226755
user2226755

Reputation: 13157

See data.feed.entry array, for get list of videos.

$.getJSON('http://gdata.youtube.com/feeds/api/users/benpearlmagic/uploads?alt=json', function(data) {
    console.log(data);
    for(var i in data.feed.entry) {
      console.log("Title : "+data.feed.entry[i].title.$t); // title
      console.log("Description : "+data.feed.entry[i].content.$t); // description
    }
});

key of data.feed.entry[i] : id, published, updated, category, title, content, link, author, gd$comments, yt$hd, media$group, gd$rating, yt$statistics.

Upvotes: 5

amman
amman

Reputation: 142

Here is your json data formatted: http://pastebin.com/M5yNzGeL Now, you need the elements of the entry array (line 97). (please look over the formatted code for better understading, you'll see it's easy :) )

And you should try jQuery.parseJSON (http://api.jquery.com/jquery.parsejson/).

Upvotes: 1

Related Questions