json - Gmail Rest API json 发送消息
问题描述
我无法向 gmail api 发送 json 请求。我可以访问独家新闻https://www.googleapis.com/auth/gmail.send,它允许我访问发送方法。
在谷歌文档中,他们声明其余部分需要以下数据。 https://developers.google.com/gmail/api/reference/rest/v1/users.messages
但我不会填满它们,要放什么?有没有人通过只发送一个json来使用api?
我在堆栈上进行了研究,但没有找到遇到这种困难的人。并且在谷歌文档上没有例子。
{
"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('\n'))
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
return JSON.stringify({
raw: encodedMessage()
});
}
console.log(createMessageJson())
推荐阅读
- multithreading - 当工作线程启动时,FMX 主线程中的动画停止
- windows - 来自 Windows 10 的 MASM 出现错误 A2006 - Visual Studio
- javascript - 使用 Jquery:我想在 div 之外放置一个 div
- java - 获取价值的 Json 文件与 DB 表
- google-apps-script - 驱动器中的 Appscript 抛出“抱歉,此时无法打开文件。请检查地址并重试”一周
- salesforce - 如何在 Salesforce 社区上渲染来自 Magnolia CMS 的图像?
- linux - Linux:将两个子字符串连接到使用 cat 读取的 infile 中的新行
- python - Pandas df.mask isnull 得到太多参数错误
- node.js - React-Bootstrap 不适用于服务器端渲染 React App?
- javascript - 如何在嵌套数组对象中删除和添加项目