使用 Google Apps 脚本插入 YouTube 顶级评论
Insert YouTube top level comment using Google Apps Script
我正在尝试使用 Google Apps 脚本创建一个程序,该程序会在某个 YouTube 频道上传时插入评论。我已经能够从该频道获取最新的 YouTube 视频 ID,但是当我尝试插入评论时,它会引发错误,"Parse Error (line 19, file 'Code')"。
第 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
});
}
根据 docs,缺少 {},并使用单引号。我现在无法对此进行测试,但希望它能解决您的问题。
commentThreadsInsert('snippet',
{},
{'snippet.channelId': '',
'snippet.videoId': '',
'snippet.topLevelComment.snippet.textOriginal': ''
});
Per Apps Script advanced services documentation, when specifying resources (such as a CommentThread
) they are the first parameter to a method. If you use the Apps Script editor's autocomplete, it is very clear about the required order:
另请注意,您错误地创建了资源主体 - 您有各种子属性。例如,snippet
属性 是 CommentThread
resource 的必需成员。三个 "snippet.___"
属性不等于一个 snippet
属性 具有 3 个子属性。
因此解决 YouTube.CommentThreads.insert
中的解析错误的解决方案是使用所需的方法签名,以及所需的资源格式:
function startCommentThread(vid, ytch, fc) {
const resource = {
snippet: {
channelId: ytch,
videoId: vid,
topLevelComment: {
snippet: {
textOriginal: fc
}
}
}
};
YouTube.CommentThreads.insert(resource, "snippet");
}
我正在尝试使用 Google Apps 脚本创建一个程序,该程序会在某个 YouTube 频道上传时插入评论。我已经能够从该频道获取最新的 YouTube 视频 ID,但是当我尝试插入评论时,它会引发错误,"Parse Error (line 19, file 'Code')"。
第 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
});
}
根据 docs,缺少 {},并使用单引号。我现在无法对此进行测试,但希望它能解决您的问题。
commentThreadsInsert('snippet',
{},
{'snippet.channelId': '',
'snippet.videoId': '',
'snippet.topLevelComment.snippet.textOriginal': ''
});
Per Apps Script advanced services documentation, when specifying resources (such as a CommentThread
) they are the first parameter to a method. If you use the Apps Script editor's autocomplete, it is very clear about the required order:
另请注意,您错误地创建了资源主体 - 您有各种子属性。例如,snippet
属性 是 CommentThread
resource 的必需成员。三个 "snippet.___"
属性不等于一个 snippet
属性 具有 3 个子属性。
因此解决 YouTube.CommentThreads.insert
中的解析错误的解决方案是使用所需的方法签名,以及所需的资源格式:
function startCommentThread(vid, ytch, fc) {
const resource = {
snippet: {
channelId: ytch,
videoId: vid,
topLevelComment: {
snippet: {
textOriginal: fc
}
}
}
};
YouTube.CommentThreads.insert(resource, "snippet");
}