我正在尝试创建 Apps 脚本 Web 应用程序,这将允许我使用此处
概述的解决方案向多个用户共享多个文件每当我尝试运行上面链接的解决方案中提供的函数(使用 UrlFetchApp 的第一个解决方案)时,它总是以错误结束(代码 404)
从记录器和调试器中我可以看到它在到达
UrlFetchApp.fetch()
序列时遇到了麻烦。
我的 GS 脚本的片段,仅对上面提供的原始链接进行了少量修改:
var userEmailsArray = ["[email protected]"]; // sample placeholder
var fileId = "File-ID"; // sample placeholder
function changePermissionBatch(role = "reader", type = "user") {
const resources = userEmailsArray.map(e => ({role: role, type: type, emailAddress: e}));
const boundary = "xxxxxxxxxx";
const payload = resources.reduce((s, e, i) => {
s += "Content-Type: application/http\r\n" +
"Content-ID: " + i + "\r\n\r\n" +
"POST https://www.googleapis.com/drive/v3/files/" + fileId + "/permissions?sendNotificationEmails=false" + "\r\n" +
"Content-Type: application/json; charset=utf-8\r\n\r\n" +
JSON.stringify(e) + "\r\n" +
"--" + boundary + "\r\n";
return s;
}, "--" + boundary + "\r\n");
const params = {
method: "post",
contentType: "multipart/mixed; boundary=" + boundary,
payload: payload,
headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
};
const res = UrlFetchApp.fetch("https://www.googleapis.com/batch", params);
console.log(res.getContentText())
}
我是否在这里遗漏了某些内容,或者是否有任何最近的政策/更新可能导致我可能不知道的请求失败?
提供的日志也没有多大帮助,因为它是一个缩短的 HTTP 响应,我环顾四周,发现没有办法根据这些日志得出错误的结论,而且我基本上在这里迷失了方向,所以任何帮助或提示将不胜感激
我找到了答案,感谢 Logan 指出了已弃用的部分。我出去发现了 Tanaike 的博客文章(在我原始问题的参考链接中提供解决方案的人)
更改并更新了以下行:
const res = UrlFetchApp.fetch("https://www.googleapis.com/batch", params);
进入:
const res = UrlFetchApp.fetch("https://www.googleapis.com/batch/drive/v3", params);
该函数现在按预期运行,并且日志没有返回错误,因此问题已解决。
来源:
链接 1 - 博客文章,其中包含示例行,其中包含代码工作所需的特定batchPath
链接 2 - 官方文档/问题公告
希望这能帮助其他可能遇到和我一样问题的人 干杯!