Just code
Just code

Reputation: 13791

How to get the Channel's list from youtube api?

I want to get all channel's list and it's statistics from the youtube api.

What I have done is:

  1. Registered console app
  2. Used Oauth Client Id
  3. https://developers.google.com/youtube/v3/docs/channels/list using this api I am getting all the statistics

This is what I am doing to get the channel's list

function requestUserUploadsPlaylistId() {
  // See https://developers.google.com/youtube/v3/docs/channels/list
  var request = gapi.client.youtube.channels.list({
    mine: true,
    part: 'statistics'
  });
  request.execute(function(response) {
    playlistId = response.result.items[0].contentDetails.relatedPlaylists.uploads;
    requestVideoPlaylist(playlistId);
  });
}

But , the problem is this return's only one channel's information. I have 20 channel's under my account. So, I need them all to display them in my table.

I have also tried to add forUsername:'Username' parameter but no success.

Also, when I tried to set managedByMe:true its returning error as

Global:"required",
Message:"Required"

Can't get any way to display the all channel's list. Can you please explain me?

This is what I want

Channelname ViewsCount
1               50
2               51

and so on.....

Ignore my code response.result.items[0] I am checking whole response in console. and items contains only one channel at a time.

Edit: enter image description here

Upvotes: 0

Views: 2214

Answers (1)

Mostafa Berg
Mostafa Berg

Reputation: 3239

Update 1:

When you use the managedByMe flag (set to true), you have to provide the required parameter onBehalfOfContentOwner for that flag, which is a string of the content owner Identifier that you have to know.

To get the proper content owner id, you need to request a new access token, and when logging in select the proper user that corresponds to the content owner id that you want to use, and things should work fine.

looking into this question it seems what you need to do is login as the CMS user account, and then you will use that user id as the content owner id, things should work afterwards as you need


Initial answer:

You are only accessing items[0] in your code which will only show one element, try changing the code to use a foreach loop and iterate over all the channels like this:

function requestUserUploadsPlaylistId() {
  // See https://developers.google.com/youtube/v3/docs/channels/list
  var request = gapi.client.youtube.channels.list({
    mine: true,
    part: 'statistics'
  });
  request.execute(function(response) {
     response.result.items.forEach(function(anItem) {
        playlistId = anItem.contentDetails.relatedPlaylists.uploads;
        requestVideoPlaylist(playlistId);
     });
  });
}

Regarding the other error, you cannot user managedByMe and mine at the same time, if you want to user managedByMe you need to clear out mine and have the proper permissions, I would recommend playing with the api explorer first to confirm everything works, then implement the code that reflects that, it's more descriptive errors there.

Upvotes: 1

Related Questions