gmail发送api失败,错误代码400在meteor中

时间:2016-02-13 11:36:44

标签: node.js meteor gmail-api

我正在尝试使用gmail api在我的meteor应用程序中发送邮件,该应用程序返回错误,

Error in calendar insert: Error: failed [400] {  "error": {   "errors": [    {        "domain": "global",     "reason": "invalidArgument",     "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"    }   ],   "code": 400,   "message": "'raw' RFC822 payload message string or uploading message via /upload/* URL required"  } } 

我已尝试过以下内容,

"sendGmail": function(str) {
    this.unblock();
        var url = "https://www.googleapis.com/gmail/v1/users/me/messages/send";

        var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');

        try {
        Meteor.http.post(url, {
            'headers' : { 
                'Authorization': "Bearer " + Meteor.user().services.google.accessToken,
                'Content-Type': 'application/json' 
            },
            'body': JSON.stringify({
                "raw": encodedMail
            })
          });
        } catch(e){
            console.log("Error in calendar insert: " + e);
        } finally {
          return true;  
        }
}

将以下字符串值作为参数传递:

    var str = "Content-Type: text/plain; charset=\"UTF-8\"\n" +
        "MIME-Version: 1.0\n" +
        "Content-Transfer-Encoding: 7bit\n" +
        "to: arunmail2u@gmail.com\n" +
        "from: arunsugan08@gmail.com\n" +
        "subject: Meteor test mail\n\n" +

    "Hi, this is test mail from meteor application";

     Meteor.call('sendGmail', str);

1 个答案:

答案 0 :(得分:1)

正文字符串的格式为content,而不是bodyCheck the documentation

  

content - 使用普通字符串并在HTTP请求正文中设置它。