here4onequestion
here4onequestion

Reputation: 85

YouTube API channel's videos list

I'm currently trying to make a site which uploads all the videos from specific playlist of some channel. Like list of videos. And I found this video - link. And that's what I have now:

$(document).ready(function() {
  $.get(
    "https://www.googleapis.com/youtube/v3/channels",{
      part : 'contentDetails', 
      forUsername : 'CHANNEL_NAME',
      key: 'MY_KEY'},
      function(data) {
        $.each( data.items, function( i, item ) {
          console.log(item);
        })
      }
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

But I have nothing in console, though judging by console.log(item) something must be there, at least undefined. And even when I write just console.log('hi') I still have nothing. I just can't understand what's wrong. Anyway, I'd be very thankful for any help.

Upvotes: 5

Views: 5010

Answers (2)

here4onequestion
here4onequestion

Reputation: 85

So if anyone wonders, here's finished code:

$(document).ready(function() {
  $.get(
    "https://www.googleapis.com/youtube/v3/search",{
      part : 'snippet', 
      channelId : 'CHANNEL_ID', // You can get one from Advanced settings on YouTube
      type : 'video',
      key: 'YOUR_KEY'},
      function(data) {
        $.each( data.items, function( i, item ) {
          $('#results').append('<li>https://www.youtube.com/embed/' + item.id.videoId + '</li>');
        })
      }
  );
});
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
  <ul id="results"></ul>
</body>
</html>

Upvotes: 3

Emre Kabaoglu
Emre Kabaoglu

Reputation: 13146

If I understood your question correctly, you want to retrieve videos of a specific channel. You should use https://www.googleapis.com/youtube/v3/search endpoint instead of https://www.googleapis.com/youtube/v3/channels. So, I provided an example with channelId and type parameters; (type parameter can be video, playlist, channel)

$(document).ready(function() {
  $.get(
    "https://www.googleapis.com/youtube/v3/search",{
      part : 'snippet', 
      channelId : 'UCR5wZcXtOUka8jTA57flzMg',
      type : 'video',
      key: 'MyKey'},
      function(data) {
        $.each( data.items, function( i, item ) {
          console.log(item);
        })
      }
  );
});

This will get you videos of a channel. If you want to get playlists of channel just change type parameter to playlist. Here is the official documentation.

Upvotes: 5

Related Questions