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

使用drive.file权限无法导出Google Drive文件的问题求助

问题:drive.file权限无法导出Google Docs文档,切换drive.readonly则正常

根据Drive V3的files.export API及API权限文档,drive.file权限理论上支持导出Drive文档,但实际测试不符合预期:在Google Docs插件中尝试导出当前文档为.docx格式时,使用drive.file权限会返回404错误,切换为drive.readonly权限则可正常运行。

测试代码

function getDocxBlob() {
  // 获取当前文档ID
  var doc = DocumentApp.getActiveDocument();
  var fileId = doc.getId();

  // 构造DOCX格式的Drive API导出URL
  var url = 'https://www.googleapis.com/drive/v3/files/' + fileId + 
            '/export?mimeType=application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  
  // 设置OAuth认证的请求选项
  var options = {
    'method': 'GET',
    'headers': {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()
    },
    'muteHttpExceptions': false // 非200响应时抛出异常
  };

  // 发起API请求
  var response = UrlFetchApp.fetch(url, options);

  // 检查请求是否成功
  if (response.getResponseCode() == 200) {
    var docxBlob = response.getBlob();
    return docxBlob; // 返回DOCX格式的Blob对象
  } else {
    // 抛出包含错误详情的异常
    throw new Error('Failed to export document: ' + response.getContentText());
  }
}

错误信息

Error: Failed to export document: {
  "error": {
    "code": 404,
    "message": "File not found: 1q3uEP9VruS3SQC2Q--79LPAzOTqKVhkE(modified docId)",
    "errors": [
      {
        "message": "File not found: 1q3uEP9VruS3SQC2Q--79LPAzOTqKVhkE(modified docId)",
        "domain": "global",
        "reason": "notFound",
        "location": "fileId",
        "locationType": "parameter"
      }
    ]
  }
}

appscript.json配置

{
  "timeZone": "Asia/Kolkata",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Drive",
        "version": "v3",
        "serviceId": "drive"
      }
    ]
  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive.file",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/script.container.ui",
    "https://www.googleapis.com/auth/documents",
    "https://www.googleapis.com/auth/script.scriptapp",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
    "openid"
  ]
}

原因分析

drive.file权限的核心限制是仅允许访问由应用本身创建,或用户通过应用流程明确授权访问的文件。在Google Docs插件场景中:

  1. 当前打开的文档并非由该插件创建,且用户没有通过插件的授权流程(比如文件选择器)主动授予对该文档的访问权限;
  2. 虽然documents权限允许插件通过DocumentApp.getActiveDocument()读取/编辑文档内容,但这一权限仅作用于Docs编辑器API,不直接覆盖Drive API的drive.file权限校验逻辑;
  3. 调用Drive V3的files.export接口时,API会严格校验drive.file的权限范围:不符合“应用创建/用户主动授权”条件的文件,会被判定为无权访问,返回伪装成“文件未找到”的404错误。

而drive.readonly权限的范围覆盖了用户所有可访问的Drive文件,因此可以绕过这一限制,正常调用导出接口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 01:22:45