Gmail REST API json发送消息

人气:816 发布:2022-10-16 标签: api rest json gmail

问题描述

向Gmail API发送json请求时遇到问题。 我可以访问独家新闻https://www.googleapis.com/auth/gmail.send,它允许我访问发送方法。

在Google文档中,他们指出,其余的需要在下面提供这些数据。 https://developers.google.com/gmail/api/reference/rest/v1/users.messages

但是我不能装满它们,要往里面放什么呢? 有没有人曾经通过REST只发送一个json来使用API?

我在堆栈上进行了研究,但没有找到有这种困难的人。和Google文档上没有任何示例。

{
  "id": string,
  "threadId": string,
  "labelIds": [
    string
  ],
  "snippet": string,
  "historyId": string,
  "internalDate": string,
  "payload": {
    object (MessagePart)
  },
  "sizeEstimate": integer,
  "raw": string
}

推荐答案

消息应编码为Base64。您可以通过将此代码粘贴到节点REPL或在线节点编译器来生成一个节点,如下所示 https://replit.com/languages/nodejs

function createMessageJson(){
    const messages = [
        'From: NAME <foo@email.com>',
        'To: Name <foobar@email.com>',
        'Content-Type: text/html; charset=utf-8',
        'MIME-Version: 1.0',
        'Subject: Re: SUBJECT',
        '',
        'BODY_TEXT',
        '',
    ];


    function encodedMessage (){
        return Buffer.from(messages.join('
'))
            .toString('base64')
            .replace(/+/g, '-')
            .replace(///g, '_')
            .replace(/=+$/, '');
    }


  return JSON.stringify({
            raw: encodedMessage()
    });
}

console.log(createMessageJson())

821