【问题标题】:Send email via Gmail API (Javascript)通过 Gmail API (Javascript) 发送电子邮件
【发布时间】:2019-01-24 18:42:57
【问题描述】:

我正在尝试通过 JS Gmail API 发送邮件,让你好世界。我已经按照this正确授权(可以列出标签)。

我正在使用以下代码,在浏览器中运行:

const message =
"From: my.email@gmail.com\r\n" + 
"To: my.email@gmail.com\r\n" +
"Subject: As basic as it gets\r\n\r\n" +
"This is the plain text body of the message.  Note the blank line between the header information and the body of the message.";


// The body needs to be base64url encoded.
const encodedMessage = btoa(message)

const reallyEncodedMessage = encodedMessage.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')

gapi.client.gmail.users.messages.send({
    userId: 'me',
    requestBody: {
        // same response with any of these
        raw: reallyEncodedMessage
        // raw: encodedMessage
        // raw: message
    }
}).then(function () { console.log("done!")});

这给出了一个 HTTP 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"
 }
}

这是使用 https://apis.google.com/js/api.js 提供的 JS API。 RFC822 示例取自 MSDNelsewhere。据我所知,对 RFC822 消息进行 Web 安全 base64 编码是此 API 的标准。 Firefox 和 Chrome 出现同样的错误。

我哪里错了?

【问题讨论】:

    标签: email google-api gmail gmail-api


    【解决方案1】:

    我认为原始数据是正确的。那么这个修改怎么样呢?

    发件人:

    requestBody: {
        // same response with any of these
        raw: reallyEncodedMessage
        // raw: encodedMessage
        // raw: message
    }
    

    收件人:

    resource: { // Modified
        // same response with any of these
        raw: reallyEncodedMessage
        // raw: encodedMessage
        // raw: message
    }
    

    如果这不起作用,请告诉我。我想考虑其他解决方案。

    【讨论】:

    • @sennett 很高兴您的问题得到了解决。也谢谢你。
    【解决方案2】:

    我不知道如何编写代码,但代码可以通过删除添加到原始语句中的主体来工作。gapi.client.gmail.users.messages.send({ userId: 'me', // resourse: { // same response with any of these raw: reallyEncodedMessage // raw: encodedMessage // raw: message // }
    我希望你试试这个,因为@Tanaike 的回答对我不起作用

    【讨论】:

    • 抱歉,没有整理好
    猜你喜欢
    • 2020-01-06
    • 2012-01-07
    • 2023-03-19
    • 2016-07-19
    • 2018-07-21
    • 2016-10-27
    • 1970-01-01
    相关资源
    最近更新 更多