使用Go解码Gmail API中的消息正文

huangapple go评论97阅读模式
英文:

Decoding the message body from the Gmail API using Go

问题

我正在尝试使用Go中的Gmail API检索消息的完整消息正文。目前,当我这样做时,我只能获取消息正文的前三个字符,即"<ht"。我相当确定问题出在消息正文的解码上,但我似乎无法弄清楚我做错了什么。

我查看了其他几种语言的示例,并尝试将它们翻译成Go,但没有成功。编码的消息正文相当大,所以我相当确定某些数据在某个地方丢失了。

这是一个(缩略)的代码片段,说明了我一直在尝试的方法:

req := svc.Users.Messages.List("me").Q("from:someone@somedomain.com,label:inbox")

r, _ := req.Do()

for _, m := range r.Messages {

msg, _ := svc.Users.Messages.Get("me", m.Id).Format("full").Do()

for _, part := range msg.Payload.Parts {

if part.MimeType == "text/html" {

  data, _ := base64.StdEncoding.DecodeString(part.Body.Data)
  html := string(data)
  fmt.Println(html)

}

}
}

英文:

I'm attempting to retrieve the full message body of messages using the Gmail API in Go. Currently when I do so, I only get the first three characters of the message body which are "&lt;ht". I'm pretty sure my issue lies with the decoding of the message body but I can't seem to figure out what I'm doing wrong.

I've looked at examples in several other languages and have tried to translate them to Go with no success. The encoded message body is rather large so I'm fairly certain some data is getting lost somewhere.

Here is an (abridged) code snippet illustrating how I've been attempting to go about this:

req := svc.Users.Messages.List(&quot;me&quot;).Q(&quot;from:someone@somedomain.com,label:inbox&quot;)

r, _ := req.Do()

for _, m := range r.Messages {

  msg, _ := svc.Users.Messages.Get(&quot;me&quot;, m.Id).Format(&quot;full&quot;).Do()

  for _, part := range msg.Payload.Parts {

    if part.MimeType == &quot;text/html&quot; {

      data, _ := base64.StdEncoding.DecodeString(part.Body.Data)
      html := string(data)
      fmt.Println(html)

    }
  }
}

答案1

得分: 10

需要使用Base64 URL编码(与标准Base64编码略有不同的字母表)。

使用相同的base64包,您应该使用:
base64.URLEncoding.DecodeString而不是
base64.StdEncoding.DecodeString

要从标准Base64获取URL Base64,请将以下内容替换:

+ 替换为 -(字符62,加号替换为破折号)
/ 替换为 _(字符63,斜杠替换为下划线)
= 替换为 * 填充

从body字符串中(源代码在这里:https://stackoverflow.com/questions/24812139/base64-decoding-of-mime-email-not-working-gmail-api和https://stackoverflow.com/questions/24460422/how-to-send-a-message-successfully-using-the-new-gmail-rest-api)。

英文:

Need to use Base64 URL encoding (slightly different alphabet than standard Base64 encoding).

Using the same base64 package, you should use:
base64.URLEncoding.DecodeString instead of
base64.StdEncoding.DecodeString.

To get URL Base64 from standard Base64, replace:

+ to - (char 62, plus to dash)
/ to _ (char 63, slash to underscore)
= to * padding

from body string (source here: https://stackoverflow.com/questions/24812139/base64-decoding-of-mime-email-not-working-gmail-api and here: https://stackoverflow.com/questions/24460422/how-to-send-a-message-successfully-using-the-new-gmail-rest-api).

huangapple
  • 本文由 发表于 2016年2月20日 00:32:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/35510179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定