I have a script bound to a Google Sheets file that is intended to create draft in Gmail with attachment (the script is attached below, I got this from other web). This requires other users to enable Gmail API both in the script as well as in the Google Developer Console.
As the owner of the file, I can do that just fine. However, when the other users try to enable Gmail API in the Google Developer Console, there is error message: "You do not have sufficient permissions to view this page".
Does anyone know what is going wrong? Is this a bug or is there anything I (as the file owner) should do?
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);
}