【问题标题】:gmail send api failed with error code 400 in meteorgmail 发送 api 失败,在流星中出现错误代码 400
【发布时间】:2016-05-24 14:11:38
【问题描述】:

我正在尝试在我的流星应用程序中使用 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);

【问题讨论】:

    标签: node.js meteor gmail-api


    【解决方案1】:

    正文字符串为content,而不是bodyCheck the documentation.

    content - 采用纯字符串并将其设置在 HTTP 请求正文中。

    【讨论】:

    • 谢谢!数据没有,但是“内容”工作正常:-)
    • @Arun 啊,你是对的 :) content 是一个纯字符串,data 接受一个对象,该对象将被库转换为字符串。我更新了答案。
    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    • 2017-06-04
    • 2021-10-16
    • 2022-11-09
    • 2021-10-11
    • 2019-12-22
    相关资源
    最近更新 更多