0
votes

I'm baffled. Best I can tell, I'm following the examples to the letter. Maybe I'm missing a parameter but I can't find what it is.

var request = gapi.client.youtube.channels.list({
  id: '<myId>',
  part: 'contentDetails'
});
request.execute(function(response) {
  console.log(response);
});

The console response has no items, but I have 3 playlists as public.

[Log] Object (learndataapi.html, line 68)

etag: "\"m2yskBQFythfE4irbTIeOgYYfBU/Rk41fm-2TD0VG1yv0-bkUvcBi9s\""

items: [] (0)

kind: "youtube#channelListResponse"

pageInfo: {totalResults: 0, resultsPerPage: 0}

result: {kind: "youtube#channelListResponse", etag: "\"m2yskBQFythfE4irbTIeOgYYfBU/Rk41fm-2TD0VG1yv0-bkUvcBi9s\"", pageInfo: {totalResults: 0, resultsPerPage: 0}, items: []}

Object Prototype

Any clues?

2

2 Answers

2
votes

Try this query :

https://www.googleapis.com/youtube/v3/search?key={your_key_here}&channelId={channel_id_here}&part=snippet,id&order=date&maxResults=30

Check here for more info: https://developers.google.com/youtube/v3/

Update: try these steps:

  1. Use the query

    https://www.googleapis.com/youtube/v3/channels?id={channel Id}key={API key}&part=contentDetails
    
  2. Use this "uploads" Id to query PlaylistItems to get the list of videos like:

    https://www.googleapis.com/youtube/v3/playlistItems?playlistId={"uploads" Id}&key={API key}&part=snippet&maxResults=50 
    

NEW UPDATE

For PlayList do these :

function httpGet(theUrl)
{
   var xmlHttp = new XMLHttpRequest();
   xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
   xmlHttp.send( null );
   return xmlHttp.responseText;
}

var response = httpGet("https://content.googleapis.com/youtube/v3/playlists?channelId=UCCTVrRB5KpIiK6V2GGVsR1Q&maxResults=50&part=snippet&key=REPLACE-WITH-YOUR-API-KEY");

console.log(response);

So in this code, you just need to add any channel Id that you want to retrieve the playlists, in this case I retrieve from a channel with this ID : UCCTVrRB5KpIiK6V2GGVsR1Q if you don't know how to get the channel ID, just check the page source of any channel you want and search for this : data-channel-external-id and use the value for channelId Parameter.

The other thing you need is the API KEY, make sure you enabled your Youtube API data and use your API KEY for key parameter.

If you send the http request, you will get a response of details for max 50 playlits, which you have access to their Id also, something like id:PLAC325451207E3105

1
votes

Well, finally! I've been through so many alternatives, I'm not sure why it wasn't this simple before. Guess I over-read something or another. Thanks to Emad for giving me the XMLHttpRequest from which I could build the gapi request.

  gapi.client.setApiKey(
    apiKey
  );

  gapi.client.load('youtube', 'v3').then(function (){
    gapi.client.youtube.playlists.list ({
      channelId: '<channelId>',
      maxResults: 50,
      part: 'snippet'
    }).then (function(response){
      console.log(response.result);
      },function(reason){
        console.log(reason);
      });
  });

Giving a response.result:

[Log] Object (learndataapi.html, line 45)
      etag: "\"m2yskBQFythfE4irbTIeOgYYfBU/Ax2NdOn2dk1o3kSplGj29Msov8Q\""
      items: Array (2)
      0 Object
         etag: "\"m2yskBQFythfE4irbTIeOgYYfBU/qQMRAPjZkOKU3LcNUxjcSiBXu8k\""
         id: "<playListId>"
         kind: "youtube#playlist"
         snippet: Object
           channelId: "<channelId>"
           channelTitle: "<channelTitle>"
           description: "<channelDescription>"
           localized: {title: "New playlist 2 with 2 videos", description: "This is new playlist 2 with 2 videos"}
           publishedAt: "2017-05-08T22:47:16.000Z"
           thumbnails: {default: {url: "https://i.ytimg.com/vi/GsNawgbVt18/default.jpg", width: 120, height: 90}, medium: {url: "https://i.ytimg.com/vi/GsNawgbVt18/mqdefault.jpg", width: 320, height: 180}, high: {url: "https://i.ytimg.com/vi/GsNawgbVt18/hqdefault.jpg", width: 480, height: 360}}
           title: "New playlist 2 with 2 videos"

...

One thing of note: The playlist is always returned in descending "publishedAt" date, so latest publications are always first... if anyone cares.