【问题标题】:Insert YouTube top level comment using Google Apps Script使用 Google Apps 脚本插入 YouTube 顶级评论
【发布时间】:2018-12-03 09:40:19
【问题描述】:

我正在尝试使用 Google Apps 脚本创建一个程序,该程序会在某个 YouTube 频道上传时插入评论。我已经能够从频道中获取最新的 YouTube 视频 ID,但是当我尝试插入评论时,它会引发错误“解析错误(第 19 行,文件'代码')”。

第 19 行:YouTube.CommentThreads.insert("snippet", {

这是我的代码:

function getVideo() {
  // MrBeast Channel ID: UCX6OQ3DkcsbYNE6H8uQQuVA
  var channel = "UCX6OQ3DkcsbYNE6H8uQQuVA";
  var fttx = "FIRST!";
  var results = YouTube.Channels.list("contentDetails", {"id": channel});
  for (var i in results.items) {
    var item = results.items[i];
    var playlistId = item.contentDetails.relatedPlaylists.uploads;
    // Uploads Playlist ID: UUX6OQ3DkcsbYNE6H8uQQuVA
    var playlistResponse = YouTube.PlaylistItems.list("snippet", {"playlistId": playlistId, "maxResults": 1});
    for (var j = 0; j < playlistResponse.items.length; j++) {
      var playlistItem = playlistResponse.items[j];
      var latvid = playlistItem.snippet.resourceId.videoId;
      comment(latvid, channel, fttx);
    }
  }
}
function comment(vid, ytch, fc) {
  YouTube.CommentThreads.insert("snippet", {
    "snippet.channelId": ytch,
    "snippet.videoId": vid,
    "snippet.topLevelComment.snippet.textOriginal": fc
  });
}

【问题讨论】:

  • 第 19 行是什么?
  • @noogui 刚刚添加了第 19 行的内容。再次检查问题。

标签: google-apps-script youtube-data-api


【解决方案1】:

Per Apps Script advanced services documentation, when specifying resources (such as a CommentThread) they are the first parameter to a method。如果你使用 Apps Script 编辑器的自动补全,那么需要的顺序就很清楚了:

还请注意,您错误地创建了资源主体 - 您有各种子属性。例如,snippet 属性是CommentThread resource 的必需成员。三个"snippet.___" 属性不等于一个具有三个子属性的snippet 属性。

因此解决YouTube.CommentThreads.insert中的解析错误的解决方案是使用所需的方法签名,以及所需的资源格式:

function startCommentThread(vid, ytch, fc) {
  const resource = {
    snippet: {
      channelId: ytch,
      videoId: vid,
      topLevelComment: {
        snippet: {
          textOriginal: fc
        }
      }
    }
  };
  YouTube.CommentThreads.insert(resource, "snippet");
}

【讨论】:

    【解决方案2】:

    根据docs,{} 缺失并使用了单引号。我现在无法对此进行测试,但希望它能解决您的问题。

      commentThreadsInsert('snippet',
          {},
          {'snippet.channelId': '',
           'snippet.videoId': '',
           'snippet.topLevelComment.snippet.textOriginal': ''
          });
    

    【讨论】:

    • 我认为这不重要。
    猜你喜欢
    • 2016-09-05
    • 2022-12-15
    • 2018-10-28
    • 2015-08-23
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多