You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用drive.revisions.list时出现访问权限失败问题求助

问题描述

我有一个绑定Google表格的AppScript,表格结构如下:

LinkMetadata
https://docs.google.com/document/d/1Lorem ipsum
https://docs.google.com/document/d/2Lorem ipsum

表格A列均为Google文档(Sheet、Slides、Doc等)的URL,后续列通过AppScript获取这些文件的元数据,如文档创建者/所有者、最近编辑记录等。

我的账号属于GSuite组织,AppScript权限绑定我的个人账号。列表中的所有文档都设置了「组织内任何人可查看/评论/编辑」权限,我个人能打开并至少查看所有文档(已逐一验证)。

但部分URL执行时出现以下错误:
GoogleJsonResponseException: API call to drive.revisions.list failed with error: The authenticated user does not have the required access to the file

我已确认:

  • 已包含https://www.googleapis.com/auth/drive权限范围
  • Drive服务已启用
  • 个人账号确实能打开这些报错的文件

之前看过相同错误的讨论但场景不符,目前无法确定问题原因,恳请提供排查方向。

参考代码
function getData() {
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OVERVIEW");
    const lastRow = sheet.getLastRow();
    const range = sheet.getRange("A2:A" + lastRow);

    for (var i = 0; i < lastRow - 1; i++) {
        const cell = range.getCell(i + 1, 1);
        const value = cell.getValue();
        const row = i + 2;

        if (value.toString() != "") {
            const id = value.split("/")[5];

            if (id) {
                // 初始化存储元数据的变量
                let title, owner, lastEditDate, lastEditorEmail, lastCommentDate, lastCommentAuthor;

                try {
                    let file;
                    try {
                        file = DriveApp.getFileById(id);
                    } catch (error) {
                        Logger.log("第" + row + "行获取文件失败: " + error);
                        continue; // 跳过当前行,处理下一行
                    }

                    // 文档标题
                    title = file.getName();

                    // 文档所有者
                    try {
                        owner = file.getOwner().getEmail();
                    } catch (error) {
                        owner = "";
                    }

                    // 最后编辑记录
                    const revisionsList = Drive.Revisions.list(id);
                    if (revisionsList && revisionsList.items.length > 0) {
                        revisionsList.items.sort(function(a, b) {
                            return new Date(b.modifiedDate) - new Date(a.modifiedDate);
                        });
                        lastEditDate = revisionsList.items[0].modifiedDate;
                        lastEditorEmail = revisionsList.items[0].lastModifyingUser.emailAddress;
                    }

                    // 最后评论记录
                    const commentsList = Drive.Comments.list(id).items;
                    if (commentsList && commentsList.length > 0) {
                        commentsList.sort(function(a, b) {
                            return new Date(b.createdDate) - new Date(a.createdDate);
                        });
                        lastCommentDate = commentsList[0].createdDate;
                        const formattedLastCommentDate = Utilities.formatDate(new Date(lastCommentDate), "GMT+1", 'dd/MM/yyyy');
                        lastCommentDate = formattedLastCommentDate;
                        lastCommentAuthor = commentsList[0].author.displayName;
                    }

                    // 格式化最后编辑日期为DD/MM/YYYY格式
                    if (lastEditDate) {
                        const formattedLastEditDate = Utilities.formatDate(new Date(lastEditDate), "GMT+1", 'dd/MM/yyyy');
                        lastEditDate = formattedLastEditDate;
                    }
                } catch (error) {
                    Logger.log("第" + row + "行获取元数据失败: " + error);
                }

                // 将获取到的元数据写入对应单元格
                sheet.getRange(row, 5).setValue(title || null);
                sheet.getRange(row, 9).setValue(owner || null);
                sheet.getRange(row, 10).setValue(lastEditDate || null);
                sheet.getRange(row, 11).setValue(lastEditorEmail || null);
                sheet.getRange(row, 12).setValue(lastCommentDate || null);
                sheet.getRange(row, 13).setValue(lastCommentAuthor || null);
            }
        }
    }
}
排查方向
  • 权限粒度差异:文件的查看/编辑权限和修订记录、评论数据的访问权限是分离的。部分文件可能被组织管理员设置了额外限制,即使你有编辑权限,也无法调用Drive.Revisions.list或Drive.Comments.list接口,可联系管理员确认组织内的权限策略。
  • 外部共享文件的限制:如果报错文件是外部账号共享到组织的,即使组织内有访问权限,文件所有者可能限制了修订记录和评论的查看权限。查看这些文件的共享详情,确认是否有额外权限限制。
  • 脚本授权异常:尽管配置了drive权限范围,AppScript可能存在权限缓存或授权未完全生效的情况。可删除脚本的授权记录(在AppScript编辑器「设置」-「项目设置」-「授权」中操作),重新运行脚本并完成授权流程。
  • 文件ID提取错误:用value.split("/")[5]提取ID的逻辑不稳定,若URL格式变化(如包含edit参数、短链接)会导致提取错误ID,进而触发权限报错。可改用正则表达式提取ID:
    const idMatch = value.match(/[-\w]{25,}/);
    const id = idMatch ? idMatch[0] : null;
    
  • API配额限制:检查是否触发了Drive API的请求配额,可在Google Cloud Console的「API和服务」-「配额」中查看Drive API的使用情况。
  • 文件类型兼容性:部分Google文件类型(如表单)对Revisions.list或Comments.list接口的支持有限,确认报错文件是否属于这类特殊类型。

内容的提问来源于stack exchange,提问作者Claudia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:10:36