最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - YouTube API channel's videos list - Stack Overflow

programmeradmin0浏览0评论

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(
    "",{
      part : 'contentDetails', 
      forUsername : 'CHANNEL_NAME',
      key: 'MY_KEY'},
      function(data) {
        $.each( data.items, function( i, item ) {
          console.log(item);
        })
      }
  );
});
<script src=".4.2/jquery.min.js"></script>

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./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./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.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 1, 2018 at 19:46 here4onequestionhere4onequestion 851 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

If I understood your question correctly, you want to retrieve videos of a specific channel. You should use https://www.googleapis./youtube/v3/search endpoint instead of https://www.googleapis./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./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.

So if anyone wonders, here's finished code:

$(document).ready(function() {
  $.get(
    "https://www.googleapis./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./embed/' + item.id.videoId + '</li>');
        })
      }
  );
});
<html>
<head>
  <script src="https://ajax.googleapis./ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
  <ul id="results"></ul>
</body>
</html>

发布评论

评论列表(0)

  1. 暂无评论