“缺少草稿消息”尝试创建草稿时出现 401 错误

发布于 2025-01-11 01:54:15 字数 1915 浏览 0 评论 0原文

尝试在 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): 尝试添加 FromToSubject 字段:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2025-01-18 01:54:15

修改点:

  • 在您的情况下,请求正文为 JSON.stringify({"message": {"raw": data}})

  • 请在请求标头中包含"Content-Type": "application/json"

  • 当您想要放置硬编码邮件文本时,请按如下方式创建请求正文。

     MIME 版本:1.0
      内容类型:多部分/替代;边界=边界边界
    
      --边界边界
      内容类型:文本/纯文本;字符集=UTF-8
      硬编码邮件
    
      --边界边界--
    
  • 并且,请将上述请求正文编码为 websafe-base64 数据。

当这些点反映在你的脚本中时,就会变成如下所示。

修改后的脚本:

var user_id = "me";
var text = "Hard Coded mail";

var message = 'MIME-Version: 1.0\r\n' +
  'Content-type: multipart/alternative; boundary=boundaryboundary\r\n\r\n' +
  '--boundaryboundary\r\n' +
  'Content-type: text/plain; charset=UTF-8\r\n' +
  text + "\r\n\r\n" +
  '--boundaryboundary--';
var blob = new Blob([message], {type:"text/plain"});
var f = new FileReader();
f.readAsDataURL(blob);
f.onload = d => {
  var base64 = d.target.result.replace(/_/g, '/').replace(/-/g, '+').split(",")[1];
  axios.post(`https://gmail.googleapis.com/gmail/v1/users/${user_id}/drafts`,
    JSON.stringify({"message": {"raw": base64}}),
    {headers: {Authorization: `Bearer ${access_token}`, "Content-Type": "application/json"}}
  ).then((res) => {
    console.log(res.data)
  }).catch((error) => {
    console.error(error.response.data.error)
  })
}

结果:

运行此脚本时,日志中会显示以下结果。

{
   "id":"###",
   "message":{
      "id":"###",
      "threadId":"###",
      "labelIds":[
         "DRAFT"
      ]
   }
}

参考:

补充:

作为另一种方法,下面修改后的脚本怎么样?在此修改中,未使用blob。并且使用message/rfc822的内容类型。在本例中,端点更改为 https://gmail.googleapis.com/upload/gmail/v1/users/${user_id}/drafts。请注意这一点。

修改后的脚本:

var user_id = "me";
var text = "Hard Coded mail";

var message = 'MIME-Version: 1.0\r\n' +
  'Content-type: multipart/alternative; boundary=boundaryboundary\r\n\r\n' +
  '--boundaryboundary\r\n' +
  'Content-type: text/plain; charset=UTF-8\r\n' +
  text + "\r\n\r\n" +
  '--boundaryboundary--';
axios.post(`https://gmail.googleapis.com/upload/gmail/v1/users/${user_id}/drafts`,
  message,
  {headers: {Authorization: `Bearer ${access_token}`, "Content-Type": "message/rfc822"}}
).then((res) => {
  console.log(JSON.stringify(res.data))
}).catch((error) => {
  console.error(error.response.data.error)
})

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.

      MIME-Version: 1.0
      Content-type: multipart/alternative; boundary=boundaryboundary
    
      --boundaryboundary
      Content-type: text/plain; charset=UTF-8
      Hard Coded mail
    
      --boundaryboundary--
    
  • 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:

var user_id = "me";
var text = "Hard Coded mail";

var message = 'MIME-Version: 1.0\r\n' +
  'Content-type: multipart/alternative; boundary=boundaryboundary\r\n\r\n' +
  '--boundaryboundary\r\n' +
  'Content-type: text/plain; charset=UTF-8\r\n' +
  text + "\r\n\r\n" +
  '--boundaryboundary--';
var blob = new Blob([message], {type:"text/plain"});
var f = new FileReader();
f.readAsDataURL(blob);
f.onload = d => {
  var base64 = d.target.result.replace(/_/g, '/').replace(/-/g, '+').split(",")[1];
  axios.post(`https://gmail.googleapis.com/gmail/v1/users/${user_id}/drafts`,
    JSON.stringify({"message": {"raw": base64}}),
    {headers: {Authorization: `Bearer ${access_token}`, "Content-Type": "application/json"}}
  ).then((res) => {
    console.log(res.data)
  }).catch((error) => {
    console.error(error.response.data.error)
  })
}

Result:

When this script is run, the following result is shown in the log.

{
   "id":"###",
   "message":{
      "id":"###",
      "threadId":"###",
      "labelIds":[
         "DRAFT"
      ]
   }
}

Reference:

Added:

As another method, how about the following modified script? In this modification, blob is not used. And the content type of message/rfc822 is used. And in this case, the endpoint is changed to https://gmail.googleapis.com/upload/gmail/v1/users/${user_id}/drafts. Please be careful about this.

Modified script:

var user_id = "me";
var text = "Hard Coded mail";

var message = 'MIME-Version: 1.0\r\n' +
  'Content-type: multipart/alternative; boundary=boundaryboundary\r\n\r\n' +
  '--boundaryboundary\r\n' +
  'Content-type: text/plain; charset=UTF-8\r\n' +
  text + "\r\n\r\n" +
  '--boundaryboundary--';
axios.post(`https://gmail.googleapis.com/upload/gmail/v1/users/${user_id}/drafts`,
  message,
  {headers: {Authorization: `Bearer ${access_token}`, "Content-Type": "message/rfc822"}}
).then((res) => {
  console.log(JSON.stringify(res.data))
}).catch((error) => {
  console.error(error.response.data.error)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文