0

尝试使用官方 GoogleAPI 解码来自 Gmail 的消息时,在尝试从full消息获取请求中解码正文数据时遇到了意外问题。

用于检索的代码full

// Fetch the email data
const gmailMessageData = await gmailService.users.messages.get({
    userId: 'me', id: message.id, format: 'full'
});

// The emails being received don't contain any parts, so I don't have any additional logic that combines parts together.
// Parse the response
const bodyData = gmailMessageData.data.payload.body.data
    .replace(/-/g, '+')
    .replace(/_/g, '/');

const decodedData = Buffer.from(bodyData, 'base64').toString('utf-8');

解码数据的值为:

<br />mail is sent as a courtesy from [redacted]. <br /> reply to: [redacted]. <br />sent on behalf of [redacted] <br /> <br />ation: [redacted] <br /> <br />ration Type: [redacted] <br />val Action: [redacted] <br /> <br /> <br />ID: [redacted] <br />

尽管raw使用以下代码使用请求检索电子邮件时:

const gmailMessageData = await gmailService.users.messages.get({
    userId: 'me', id: message.id, format: 'raw'
});

const rawData = gmailMessageData.data.raw;

const decodedData = Buffer.from(rawData, 'base64url').toString('utf-8');

解码数据的值是(特别关注正文,忽略需要进行的一些格式化/替换):

This email is sent as a courtesy from [redacted].=0D<br />=0APlease rep= ly to: [redacted].=0D<br />=0AEmail sent on behalf = of [redacted]=0D<br />=0A=0D<br />=0AApplication: [redacted]= =0D<br />=0A=0D<br />=0ARegistration Type: [redacted] =0D<br />=0A= Retrieval Action: [redacted] =0D<br />=0A=0D<b= r />=0A=0D<br />=0AGuest ID:[redacted]=0D<br />=0A=0D<br />

raw解码时的数据体与预期的实际数据一致,而full解码后的数据似乎缺少字符。

我尝试了下面列出的几个库,但仍然无法解决缺少字符的问题:

  • base64url
  • js-base64
  • urlsafe-base64

我也尝试过直接从代码片段base64urlBuffer.from方法中解码full以放弃替换,但仍然无济于事。

我已经在以下平台上测试了这段代码,并且都产生了相同的结果,所以我不认为它与系统相关:

  • Windows 10 20H2 - 节点 16
  • MacOS 12.0.1 - 节点 17
  • CentOS 8 - 节点 17
  • Ubuntu 20.04 - 节点 17
  • Node-17-alpine Docker 容器

这似乎也与输出变量无关,因为我使用 express 输出到 Web 浏览器,直接输出到控制台,甚至输出到文件;所有三个产生相同的输出。

在这一点上,我不知道还有什么可以尝试的。

编辑跟进:按照建议从full片段中删除replace()s,直接进入base64,甚至base64url解码后,我仍然遇到同样的问题;甚至贯穿上述所有系统。

4

1 回答 1

0

您的 replace() 导致了问题。你只需要base64解码。

于 2022-01-04T20:29:14.620 回答