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

部署Google Apps Script可执行API时遇Error 404错误求助

问题根源

你混淆了两种完全不同的Google服务使用场景:你的代码是直接调用Google Drive API的files.copy接口复制演示文稿,但错误地把它部署成了Google Apps Script可执行API,导致请求路径完全不匹配,触发404错误。

正确解决步骤

1. 废弃Apps Script可执行API部署

你的代码逻辑是前端直接调用Drive API,不需要将这段代码做成可执行API部署。可执行API是给外部服务调用你的Apps Script函数用的,和当前需求完全不匹配。

2. 配置前端Drive API访问环境

  • 先在Google Cloud Console里启用Google Drive API,创建类型为「Web应用」的OAuth客户端ID。
  • 前端加载gapi后,先初始化授权并请求drive权限:
gapi.load('client:auth2', () => {
  gapi.auth2.init({
    client_id: '你的OAuth客户端ID',
    scope: 'https://www.googleapis.com/auth/drive'
  }).then(() => {
    // 在这里执行你的复制代码
  });
});

3. 修正代码的缺失项

你的现有代码需要补全几个关键部分:

// 定义必填参数
const presentationId = '你的源演示文稿ID';
const copyTitle = '复制后的演示文稿名称';
const callback = (copyId) => {
  // 复制成功后的处理逻辑,比如提示用户
  console.log('复制完成,文件ID:', copyId);
};

const request = { name: copyTitle };
gapi.client.drive.files.copy({
  fileId: presentationId,
  resource: request,
}).then((driveResponse) => {
  const presentationCopyId = driveResponse.result.id;
  if (callback) callback(presentationCopyId);
  console.log('create copy_presentation with id', presentationCopyId);
}).catch((err) => {
  document.getElementById('content').innerText = err.message;
});

注意:原代码的try/catch只能捕获同步错误,Promise的异步错误要放在.catch()里处理。

4. 验证权限与配额

  • 确保当前登录的Google账号对源演示文稿有可复制权限(至少是查看权限,编辑权限更稳妥)
  • 检查Cloud Console中Drive API的配额是否正常(免费配额足够日常测试)
额外场景说明

如果你的需求是通过后端服务完成复制(不是前端直接调用),那才需要用Apps Script部署可执行API,此时代码要改用Apps Script的DriveApp服务:

function copyPresentation(presentationId, copyTitle) {
  const file = DriveApp.getFileById(presentationId);
  const copy = file.makeCopy(copyTitle);
  return copy.getId();
}

这种情况下部署后的调用URL才会是/v1/scripts/...:run,且需要用正确的授权方式发起请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 07:48:45