【发布时间】: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 示例取自 MSDN 和 elsewhere。据我所知,对 RFC822 消息进行 Web 安全 base64 编码是此 API 的标准。 Firefox 和 Chrome 出现同样的错误。
我哪里错了?
【问题讨论】:
标签: email google-api gmail gmail-api