【发布时间】:2021-11-28 09:24:33
【问题描述】:
我正在尝试使用 Gmail 高级服务create a Gmail draft。我需要的是让body 在bytes format 中包含data。我构建了以下功能:
const createDraftWithAdvancedService = () => {
Gmail.Users.Drafts.create({
message: {
payload: {
parts: [
{
body: {
data: [
42,
123,
123,
80,
114,
],
}
}
],
headers: [
{
"value": "This is a test subject",
"name": "Subject"
},
]
}
}
}, 'user@domain.com');
}
但是,当我运行它时,我收到以下错误:
GoogleJsonResponseException:对 gmail.users.drafts.create 的 API 调用失败并出现错误:收到的 JSON 负载无效。 'draft.message.payload.parts[0].body' 中的未知名称“数据”:原始字段不重复,无法启动列表。
这个错误看起来很奇怪,因为我正确地遵循了索引(或者我在检查一百次之后相信)。
我在这里错过了什么?
更新
我将字节字符串格式化为数组的原因是,当您阅读草稿时,这就是 Gmail API 返回的内容。如果您有不同的工作代码,我会全力以赴。而且我不能使用raw,我需要设置字节字符串。
这是一个如何检索消息对象及其格式的示例:
我的草稿消息:
检索此草稿消息的脚本:
const getMessage = () => {
const id = 'r-8326849559354985208';
const msg = Gmail.Users.Drafts.get('user@domain.com', id);
console.log(JSON.stringify(msg, null, 2));
}
脚本的输出:
{
"message": {
"internalDate": "1633701716000",
"snippet": "Draft body",
"labelIds": [
"DRAFT"
],
"historyId": "954861",
"sizeEstimate": 534,
"payload": {
"filename": "",
"parts": [
{
"partId": "0",
"headers": [
{
"value": "text/plain; charset=\"UTF-8\"",
"name": "Content-Type"
}
],
"filename": "",
"body": {
"data": [
68,
114,
97,
102,
116,
32,
98,
111,
100,
121,
13,
10
],
"size": 12
},
"mimeType": "text/plain"
},
{
"headers": [
{
"value": "text/html; charset=\"UTF-8\"",
"name": "Content-Type"
}
],
"partId": "1",
"body": {
"size": 33,
"data": [
60,
100,
105,
118,
32,
100,
105,
114,
61,
34,
108,
116,
114,
34,
62,
68,
114,
97,
102,
116,
32,
98,
111,
100,
121,
60,
47,
100,
105,
118,
62,
13,
10
]
},
"mimeType": "text/html",
"filename": ""
}
],
"body": {
"size": 0
},
"headers": [
{
"value": "1.0",
"name": "MIME-Version"
},
{
"value": "Fri, 8 Oct 2021 16:01:56 +0200",
"name": "Date"
},
{
"value": "<CADVhnimBt3Jdod1wBgGUgB_75yrsoJMwM68mtYKmX6cN39=CNQ@mail.gmail.com>",
"name": "Message-ID"
},
{
"name": "Subject",
"value": "Draft subject"
},
{
"name": "From",
"value": "\"KOSTYUK, Dmitry\" <user@domain.com>"
},
{
"value": "multipart/alternative; boundary=\"00000000000088918105cdd7d2e1\"",
"name": "Content-Type"
}
],
"mimeType": "multipart/alternative",
"partId": ""
},
"id": "17c6035e45454be8",
"threadId": "17c6035c50e83b2f"
},
"id": "r-8326849559354985208"
}
【问题讨论】:
-
> base64 编码的字符串。
-
应该是
string。我认为您不应该使用payload,而应该使用message:{raw:"b64string"} -
见python guide。你需要
raw。创建没有库的 MIMEText 有点困难,但可行。我认为@Tanaike 对创建符合 RFC2822 的 MIMEText 有一些答案 -
给你go
-
@TheMaster 谢谢,我能够在引用的答案中调整高级 Gmail 服务部分,以使事情对我有用。我会按照你的建议尽快写一个答案。 StackOverflow 似乎是唯一记录此行为的地方。我现在也必须弄清楚附件,但这是一个不同的问题
标签: google-apps-script gmail gmail-api emoji