问题
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