Google Appscripts“权限不足”错误

时间:2015-11-09 23:37:55

标签: gmail-api

当我尝试使用google appscripts创建草稿时出现以下错误

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "insufficientPermissions",
    "message": "Insufficient Permission"
   }
  ],
  "code": 403,
  "message": "Insufficient Permission"
 }
}

我在资源中启用了GMAIL api服务 - >先进的谷歌服务。我使用以下结束点来创建草稿。

https://www.googleapis.com/gmail/v1/users/me/drafts

我不确定是否需要启用任何其他api来完成这项工作。非常感谢调试此问题的任何帮助

我正在制作api,如下所示:

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

}

2 个答案:

答案 0 :(得分:1)

您需要授权您的应用获取访问权限, 你只需要在SCOPES数组中添加值

public static void main(String str[]) {
    String fun="M(a,b,c);"; 
    String inputset;
    char m,op;
    fun=fun.substring(0, fun.length()-2);
    //Vector<String> input=new Vector<String>();
    int indx=0;
    String cp;
    cp = ")";
    if(fun.charAt(0)=='M' &&  fun.charAt(1)=='('){
        int lcp=fun.lastIndexOf(cp);
        int ncp=fun.indexOf(cp);
        System.out.println("lcp-   "+lcp+"  ncp-   "+ncp);
    }
}

答案 1 :(得分:0)

您获得的错误与凭据有关。虽然您要发送令牌,但该令牌无法访问Gmail。

在Apps脚本中,转到菜单“资源&gt;推进Google服务”

查找Gmail服务并启用它。

然后点击文本链接“还必须在Google Developers Console中启用这些服务。”

它将打开开发者控制台,您还必须启用Gmail API。

完成此操作后,再次运行您的代码,它将要求获得访问Gmail的权限。 现在令牌可以访问Gmail API。

如果它不适合你,请告诉我。