首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >通过Gmail API (Javascript)发送电子邮件

通过Gmail API (Javascript)发送电子邮件
EN

Stack Overflow用户
提问于 2018-08-18 01:17:28
回答 1查看 9.2K关注 0票数 3

我正在尝试通过JS Gmail API发送邮件来获取hello world。根据this,我已正确授权(可以列出标签)。

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

代码语言:javascript
复制
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响应:

代码语言:javascript
复制
{
 "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。取自MSDNelsewhere的RFC822示例。据我所知,对RFC822消息进行网页安全的base64编码是这个API的标准。在Firefox和Chrome中也有同样的错误。

我哪里错了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-18 06:02:34

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

发自:

代码语言:javascript
复制
requestBody: {
    // same response with any of these
    raw: reallyEncodedMessage
    // raw: encodedMessage
    // raw: message
}

至:

代码语言:javascript
复制
resource: { // Modified
    // same response with any of these
    raw: reallyEncodedMessage
    // raw: encodedMessage
    // raw: message
}

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51900190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档