3

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Shihan Khan
  • 2,180
  • 4
  • 34
  • 67
  • If I run this script, it got error with the message that it can't find Youtube object in the first line. How can I import the object? Is there something I have to declare in advance? – verystrongjoe Dec 23 '16 at 10:01

1 Answers1

3

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
  });
Kriggs
  • 3,731
  • 1
  • 15
  • 23
  • Thanks, I'm getting the titles but not the video ids. Any idea how can I fix this? – Shihan Khan Feb 02 '16 at 05:56
  • N. B. - I'm getting the playlist's titles and if I use `item.id` then I can get the playlist's ids. But I was looking for video titles and ids. How can I achieve that? Thanks. – Shihan Khan Feb 02 '16 at 06:08
  • Here is the method to get all videos using channel url http://www.gappsscript.com/get-a-list-of-videos-in-a-channel-using-youtube-service/ – Ashot Khachatryan Mar 13 '17 at 14:57
  • This solution provided the video IDs for me. https://stackoverflow.com/questions/66965119/how-to-get-information-about-all-videos-of-a-channel-to-google-sheets-with-googl/66965120#66965120 – user14915635 Jun 03 '22 at 14:10