List all videos of a Youtube channel in Google Apps Script

£可爱£侵袭症+ 提交于 2019-12-08 07:06:42

问题


I'm using Youtube Data API to list first 25 video titles and ids of a channel in google spreadsheet, but for some reason I'm only getting the Title of the channel. Here are my codes,

function searchByChannel() {
    var results = YouTube.Channels.list('id,snippet', {
        id: 'UC-9-kyTW8ZkZNDHQJ6FgpwQ',
        maxResults: 25
    });

    var title = "";
    var id = "";
    var lr = 0;
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet1 = ss.getSheetByName("Sheet1");
    for (var i = 0; i < results.items.length; i++) {
        var item = results.items[i];
        title = item.snippet.title;
        id = item.id.videoId;
        lr = sheet1.getLastRow() + 1;
        sheet1.getRange(lr, 1).setValue(title);
        sheet1.getRange(lr, 2).setValue(id);
        lr++;
    }
}

I'm new to youtube data api so I don't know what I'm doing wrong here. How can I list 25 video ids and titles in my spreadsheet using google apps script? Need this help badly! Thanks.


回答1:


The documentation for the YouTube.Channels states that it will return information about the Channel, nothing about the videos, that's why your getting the channel's info.

YouTube.Search on the other hand gather info about videos, and you can feed it a channelId parameter that will seek into a specified channel, as such, your result code should look like:

  var results = YouTube.Search.list('id,snippet', {
    channelId:'UC-9-kyTW8ZkZNDHQJ6FgpwQ',
    maxResults: 25
  });


来源:https://stackoverflow.com/questions/35111170/list-all-videos-of-a-youtube-channel-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!