【问题标题】:How to Create YouTube Playlist with Google Script and Sheets如何使用 Google Script 和 Sheets 创建 YouTube 播放列表
【发布时间】:2020-08-15 06:04:20
【问题描述】:

如何使这个 google 表格脚本工作以更新现有的 Youtube 播放列表。

function addVideoToYouTubePlaylist() {
  // Read the source videos from Google Sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();

  // Add your own playlist Id here
  var playlistId = "PLAYLIST_ID_HERE";

  // iterate through all rows in the sheet
  for (var d=1,l=data.length; d

【问题讨论】:

  • 您尝试过什么吗?你卡在哪里了?

标签: google-apps-script google-sheets youtube youtube-api playlist


【解决方案1】:

解决方案:

这就是你要找的:

function updateYTPlaylist() {
  
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const yt_video_ids = sh.getRange('A2:A'+sh.getLastRow()).getValues().flat([1]);
  
  const playlistId = "PLAYLIST_ID_HERE";
  
  yt_video_ids.forEach( vid => 
                       
     YouTube.PlaylistItems.insert({
      snippet: {
        playlistId: playlistId,
        resourceId: {
          kind: "youtube#video",
          videoId: vid
        }
      }
    }, "snippet"));

   Utilities.sleep(2000);                                                             
}

解释:

我假设您的工作表中有一个 YouTube 视频 ID 列表,名称为 Sheet1,范围为 A2:A,如下所示:

上述代码将遍历此列表 yt_video_ids,并将每个视频 ID 添加到由 playlistId 定义的选定播放列表中。

资源:

您需要在 google 脚本编辑器中点击 Resources => Advanced Google Services,然后启用 YouTube Data API v3(截至现在这是最新版本)。

【讨论】:

  • 谢谢。看来我快到了。它给了我以下错误: GoogleJsonResponseException: API call to youtube.playlistItems.insert failed with error: Forbidden (line 11, file "Code") LINE 11 如下: YouTube.PlaylistItems.insert({
  • 您是否允许权限?
  • 是的,我启用了 YouTube Data API v3
  • 服务帐户不适用于 YouTube 数据 API 调用,因为服务帐户需要关联的 YouTube 频道,并且您不能将新的或现有的频道与服务帐户相关联。如果您使用服务帐户调用 YouTube 数据 API,API 服务器会返回一个错误,错误类型设置为未授权,原因设置为 youtubeSignupRequired。也许这是你的问题。
  • 我将此问题标记为已回答,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2018-01-15
  • 2014-01-16
  • 2011-07-04
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多