I've been trying to get a video duration with Youtube JavaScript API v3, this is the relevant part of my JS:
var request = gapi.client.youtube.search.list({
q: 'eminem',
part: 'snippet'
});
request.execute(function(response) {
$('#results').empty()
var srchItems = response.result.items;
$.each(srchItems, function(index, item) {
vidTitle = item.snippet.title;
vidTime = item.contentDetails.duration;
vidThumburl = item.snippet.thumbnails.default.url;
$('#results').append('<pre>' + vidTitle + vidTime +'</pre>');
});
});
The problem is in vidTime = item.contentDetails.duration;
and console returns the following error:
Uncaught TypeError: Cannot read property 'duration' of undefined
But, i'm looking the JSON structure returned in the request (See Here) and it was to be working, and I don't know why i'm getting this error :(
I've tried to change the part
to: part: snippet, contentDetails
but with this change i've more errors...
So, how to do to fix this?
EDITED
To analyze the return of console.log(response);
I could see that the contentDetails is not returned in the array, but then how can I get the video duration?