node.js - meteor 中的 gmail 发送 api 失败,错误代码为 400

标签 node.js meteor gmail-api

我试图在我的meteor应用程序中使用gmail api发送邮件,它返回以下错误,

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);

最佳答案

正文字符串以 content 形式给出,而不是 bodyCheck the documentation .

content - Takes a plain String and sets it on the HTTP request body.

关于node.js - meteor 中的 gmail 发送 api 失败,错误代码为 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35379351/

相关文章:

javascript - node.js csurf 无效的 csrf token

node.js - NodeJS 实际上使用 OAuth (Twitter)

javascript - 在 Meteor 中只允许单击一次按钮

api - 如何从 Gmail 的主收件箱中获取未读邮件?

gmail-api - GMAIL API 在 C# 中发送带附件的电子邮件

node.js - 指定node.js中是否存在文件

node.js - 如何在 Node.js 脚本中分离生成的子进程?

android - Meteor android apk 显示空白屏幕

node.js - 如何设置环境变量以永远运行 meteorjs

gmail-api - 如何通过消息ID查询草稿?