“缺少草稿消息”尝试创建草稿时出现 401 错误
尝试在 https://gmail.googleapis.com/gmail/v1/users/${user_id}/drafts
上进行 POST
时,为了创建 Gmail 草稿,我收到缺少草稿消息
错误提示。
这是发出请求的实际代码:
let userMail = axios.post(`https://gmail.googleapis.com/gmail/v1/users/${user_id}/drafts`,
{
body: {
draft: {
message: {
raw: "Hard Coded mail",
}
}
}
},
{
headers: {
Authorization: `Bearer ${access_token}`,
}
})
console.log(userMail)
console.log(userMail)
显示了一堆信息,例如:
data: {
error: {
code: 400,
message: 'Missing draft message',
errors: [
{
message: 'Missing draft message',
domain: 'global',
reason: 'invalidArgument'
}
],
status: 'INVALID_ARGUMENT'
}
}
我是否缺少请求的 body
中的某些内容,或者语法不正确?
根据此线程 缺少草稿message - javascript Gmail API - 如何构建请求正文?,
“请求的正确结构:”
'draft': {
'message': {
'raw': base64EncodedEmail
}
}
PS:我没有使用外部节点模块,保持“普通”
编辑(1): 尝试添加 From
、To
和 Subject
字段:
var message = 'MIME-Version: 1.0\r\n' +
'Content-type: multipart/alternative; boundary=boundaryboundary\r\n\r\n' +
'From: ' + from + "\r\n" +
'To: ' + to + "\r\n" +
'Subject: ' + subject + "\r\n" +
'--boundaryboundary\r\n' +
'Content-type: text/plain; charset=UTF-8\r\n' +
mailContent + "\r\n\r\n" +
'--boundaryboundary--';
While trying to POST
at https://gmail.googleapis.com/gmail/v1/users/${user_id}/drafts
, in order to create a gmail draft, I get prompt with a Missing draft message
error.
This is the actual code that make the request:
let userMail = axios.post(`https://gmail.googleapis.com/gmail/v1/users/${user_id}/drafts`,
{
body: {
draft: {
message: {
raw: "Hard Coded mail",
}
}
}
},
{
headers: {
Authorization: `Bearer ${access_token}`,
}
})
console.log(userMail)
The console.log(userMail)
show's a bunch of information such as:
data: {
error: {
code: 400,
message: 'Missing draft message',
errors: [
{
message: 'Missing draft message',
domain: 'global',
reason: 'invalidArgument'
}
],
status: 'INVALID_ARGUMENT'
}
}
Am I missing on something in the body
of the request, or is the syntax incorrect ?
According to this thread Missing draft message - javascript Gmail API - how to structure body of the request?,
"The correct structure of the request:"
'draft': {
'message': {
'raw': base64EncodedEmail
}
}
PS: I'm not using external node modules, keeping it "vanilla"
EDIT (1):
Trying to add From
, To
and Subject
fields:
var message = 'MIME-Version: 1.0\r\n' +
'Content-type: multipart/alternative; boundary=boundaryboundary\r\n\r\n' +
'From: ' + from + "\r\n" +
'To: ' + to + "\r\n" +
'Subject: ' + subject + "\r\n" +
'--boundaryboundary\r\n' +
'Content-type: text/plain; charset=UTF-8\r\n' +
mailContent + "\r\n\r\n" +
'--boundaryboundary--';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改点:
在您的情况下,请求正文为
JSON.stringify({"message": {"raw": data}})
。请在请求标头中包含
"Content-Type": "application/json"
。当您想要放置
硬编码邮件
文本时,请按如下方式创建请求正文。并且,请将上述请求正文编码为 websafe-base64 数据。
当这些点反映在你的脚本中时,就会变成如下所示。
修改后的脚本:
结果:
运行此脚本时,日志中会显示以下结果。
参考:
补充:
作为另一种方法,下面修改后的脚本怎么样?在此修改中,未使用
blob
。并且使用message/rfc822
的内容类型。在本例中,端点更改为https://gmail.googleapis.com/upload/gmail/v1/users/${user_id}/drafts
。请注意这一点。修改后的脚本:
Modification points:
In your situation, the request body is
JSON.stringify({"message": {"raw": data}})
.Please include
"Content-Type": "application/json"
in the request header.When you want to put a text of
Hard Coded mail
, please create the request body as follows.And, please encode the above request body to the websafe-base64 data.
When these points are reflected in your script, it becomes as follows.
Modified script:
Result:
When this script is run, the following result is shown in the log.
Reference:
Added:
As another method, how about the following modified script? In this modification,
blob
is not used. And the content type ofmessage/rfc822
is used. And in this case, the endpoint is changed tohttps://gmail.googleapis.com/upload/gmail/v1/users/${user_id}/drafts
. Please be careful about this.Modified script: