使用YouTube Data API与Google Apps Script无法更新自有评论求助
问题描述
使用Google Apps Script结合Google Sheets更新自身YouTube账号下的评论时,触发权限错误:
由于权限不足,无法更新评论。请求可能未正确授权。
已完成的配置:
- 使用本人Google/YouTube账号操作自有评论
- 在
appsscript.json中配置了授权范围https://www.googleapis.com/auth/youtube.force-ssl - 已启用YouTube Data API v3高级服务
- 参考过Stack Overflow同类问题,但未找到适配Google Sheets场景的解决方案
核心代码
评论更新逻辑
// Text from the comment_ID "": /* The Magic Cylinder animation is real or am edit? * Honest question, I don't play MD, just looking video edits xD */ /** * Comment (reply) to send as body request. * Sample body - took from YouTube Data API documentation: */ var myCommentBodyRequest = { "id": "UgyBzIHN1fFHveTCRb14AaABAg", // <= 目标评论ID "snippet": { "textOriginal": "The Magic Cylinder animation is real or an edit?" // <= 更新后的评论内容 } }; // 更新评论 var commentCreatedResponse = YouTube.Comments.update(myCommentBodyRequest, "snippet"); // 日志输出结果 Logger.log(commentCreatedResponse); // 更新表格单元格内容 sheet.getRange("E" + sheet.getCurrentCell().getRow()).setValue(commentText); // 弹出成功提示 ui.alert("The comment was updated successfully.");
appsscript.json配置
{ "timeZone": "America/Bogota", "dependencies": { "enabledAdvancedServices": [ { "userSymbol": "YouTube", "version": "v3", "serviceId": "youtube" } ] }, "exceptionLogging": "STACKDRIVER", "runtimeVersion": "V8", "oauthScopes": [ "https://www.googleapis.com/auth/spreadsheets.currentonly", "https://www.googleapis.com/auth/spreadsheets", "https://www.googleapis.com/auth/script.container.ui", "https://www.googleapis.com/auth/youtube.force-ssl" ] }
已提交Google Issue Tracker工单,怀疑是否需要谷歌审核脚本但无依据,询问是否存在遗漏的配置或步骤。
排查与解决方案
1. 验证评论ID有效性
确保myCommentBodyRequest中的id是你发布的顶级评论ID,而非评论回复的ID。YouTube Data API更新评论时,需严格匹配评论的唯一ID,若为回复评论,需确认ID对应你发布的回复条目,而非父评论ID。
2. 重新触发授权流程
即使已配置权限范围,仍可能存在授权缓存问题:
- 打开脚本编辑器,点击「运行」选择对应执行函数
- 在授权弹窗中重新完成授权,若出现「未验证的应用」提示,点击「高级」→「前往脚本(不安全)」完成授权(个人脚本的正常流程)
3. 确认YouTube Data API启用状态
除了在Apps Script中启用高级服务,还需在Google Cloud Console中确认对应项目已启用YouTube Data API v3:
- 在脚本编辑器的「资源」→「云平台项目」中查看关联的项目ID
- 登录Google Cloud Console,找到该项目,在API库中搜索「YouTube Data API v3」,确认状态为「已启用」
4. 补全代码缺失部分
代码中sheet、ui、commentText变量未定义,需补充初始化逻辑,否则会触发额外错误:
// 初始化表格与UI对象 var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var ui = SpreadsheetApp.getUi(); // 绑定更新后的评论文本 var commentText = myCommentBodyRequest.snippet.textOriginal;
5. 排查反滥用限制
若上述步骤均完成仍报错,可能是Google的反滥用机制限制:
- 手动在YouTube网页端编辑该评论,确认账号本身无权限异常
- 降低脚本执行频率,避免短时间内多次调用API
- 若问题持续,在Google Issue Tracker工单中补充账号状态、脚本执行场景等细节,等待官方回复
内容的提问来源于stack exchange,提问作者Marco Aurelio Fernandez Reyes
相关产品推荐
相关产品推荐

