我有一个绑定到 Google 表格文件的脚本,该文件旨在在 Gmail 中创建带有附件的草稿(脚本附在下面,我从其他网站获得)。这需要其他用户在脚本和 Google Developer Console 中都启用 Gmail API。
作为文件的所有者,我可以做到这一点。但是,当其他用户尝试在 Google Developer Console 中启用 Gmail API 时,会出现错误消息:“您没有足够的权限查看此页面”。
有谁知道出了什么问题?这是一个错误还是我(作为文件所有者)应该做些什么?
function callGmailAPI_(message) {
var payload = createMimeMessage_(message);
var response = UrlFetchApp.fetch(
"https://www.googleapis.com/upload/gmail/v1/users/me/drafts?uploadType=media", {
method: "POST",
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken(),
"Content-Type": "message/rfc822",
},
muteHttpExceptions: true,
payload: payload
});
Logger.log(response.getResponseCode());
Logger.log(response.getContentText());
}
function encode_(subject) {
var enc_subject = Utilities.base64Encode(subject, Utilities.Charset.UTF_8);
return '=?utf-8?B?' + enc_subject + '?=';
}
function createMimeMessage_(msg) {
var nl = "\n";
var boundary = "__test_dot_com__";
var mimeBody = [
"MIME-Version: 1.0",
"To: " + msg.to.email,
"Cc: " + msg.cc.email,
"Subject: " + encode_(msg.subject),
"Content-Type: multipart/alternative; boundary=" + boundary + nl,
"--" + boundary,
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: base64" + nl,
Utilities.base64Encode(msg.body.text, Utilities.Charset.UTF_8) + nl,
"--" + boundary,
"Content-Type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: base64" + nl,
Utilities.base64Encode(msg.body.html, Utilities.Charset.UTF_8) + nl
];
for (var i = 0; i < msg.files.length; i++) {
var attachment = [
"--" + boundary,
"Content-Type: " + msg.files[i].mimeType + '; name="' + msg.files[i].fileName + '"',
'Content-Disposition: attachment; filename="' + msg.files[i].fileName + '"',
"Content-Transfer-Encoding: base64" + nl,
msg.files[i].bytes
];
mimeBody.push(attachment.join(nl));
}
mimeBody.push("--" + boundary + "--");
return mimeBody.join(nl);
}