问题
I get following error when I try to create draft using google appscripts
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
I enabled GMAIL api service in resource --> Advanced google services. I am using the following end point to create the draft.
https://www.googleapis.com/gmail/v1/users/me/drafts
I am not sure if I need to enable any other api to make this work. Would appreciate any help to debug this issue
I am making the api as shown below:
function createDraft(tid,Subject,Body,fromEmail,toEmail,ccEmail) {
var boundary = "ABDADASFDSBCCCADSAD" + (new Date()).getTime().toString();
SendSubject = "Subject:" + Subject
raw = SendSubject + "\r\n" +
"From: " + fromEmail + "\r\n" +
"To: " + toEmail + "\r\n" +
"Cc: " + ccEmail + "\r\n" +
"Content-Type: multipart/alternative; boundary=" + boundary + "\r\n" + "\r\n" +
"--" + boundary + "\r\n" +
"Content-Type: text/html; charset=UTF-8" + "\r\n" +
"Content-Transfer-Encoding: quoted-printable" + "\r\n" + "\r\n" +
Body + "\r\n" + "\r\n" +
"--" + boundary + "--"
//var draftBody = Utilities.base64Encode(raw, Utilities.Charset.US_ASCII).replace(/\//g,'_').replace(/\+/g,'-')
var draftBody = Utilities.base64EncodeWebSafe(raw, Utilities.Charset.UTF_8)
var params = {method:"post",
contentType: "application/json",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken(), "Reply-To": "naveen@skedool.it"},
muteHttpExceptions:true,
payload:JSON.stringify({
"message": {
"raw": draftBody,
"threadId" : tid
}
})
};
var resp = UrlFetchApp.fetch("https://www.googleapis.com/gmail/v1/users/me/drafts", params);
//Logger.log(resp);
return resp
}
回答1:
The error you are getting is related to the credentials. although you are sending a token, that token does not have access to Gmail.
In Apps script go to the menu "Resources > Advances Google Services"
There look for the Gmail service and enable it.
Then click on the link of the text "These services must also be enabled in the Google Developers Console."
It will open the Developer Console, and also there you have to enable the Gmail API.
After doing that, run your code again and it will ask for permission to access Gmail. Now the token will have access to Gmail API.
Let me know if it didn't work for you.
回答2:
you need to give permission to your app to get the access, you just need to add valued in SCOPES array
private static final String[] SCOPES = { GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.MAIL_GOOGLE_COM};
mCredential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
来源:https://stackoverflow.com/questions/33620359/insufficient-permissions-error-with-google-appscripts