在GMail API中获取附件

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

Get attachments in GMail API

问题

我正在尝试从Golang中的邮件中获取附件。问题出在从Gmail中读取的base64负载上,给我报错:

输入字节13处的非法base64数据

这是我的代码(省略了错误处理部分)
..

attach, _ := srv.Users.Messages.Attachments.Get(user, messageid, attachmentid).Do()
decoded, err := base64.StdEncoding.DecodeString(attach.Data)

这会抛出上述错误,如果我在Gmail中查看原始消息,可以在标题之后看到这个:

begin 644 filename-of-the-attachment.extension
M'XL(`/Y;GU8``^S]R[(>R9&E"\[[*5)JVI*6;N9WS(_TD3/J0<U:>H`*;F9"...

感谢任何帮助!
谢谢

英文:

I am trying to get an attachment from a mail in golang. The problem is in the base64 payload read from Gmail giving me the error

illegal base64 data at input byte 13

Here's my code (err handling omitted)
..

attach, _ := srv.Users.Messages.Attachments.Get(user, messageid, attachmentid).Do()
decoded, err := base64.StdEncoding.DecodeString(attach.Data)

This throws the mentioned error and if I look at the original message in GMail can see after the headers this:

begin 644 filename-of-the-attachment.extension
M'XL(`/Y;GU8``^S]R[(>R9&E"\[[*5)JVI*6;N9WS(_TD3/J0<U:>H`*;F9"...

Any help appreciated
Thanks

答案1

得分: 5

问题出在base64编码上:根据文档所说,payload(无论是“full”模式还是“raw”模式)应该使用base64URL编码,而不是base64编码。
所以这段代码是有效的:

attach, _ := srv.Users.Messages.Attachments.Get(user, messageid, attachmentid).Do()
decoded, err := base64.URLEncoding.DecodeString(attach.Data)
fileout, err := os.OpenFile(....

话虽如此,我发现默认的“full”模式更容易处理
在GMail API中获取附件

英文:

The problem is in the base64 encoding: as the documentation say the payload (either in "full" or "raw" mode) is in base64URL encoding, not base64.
So this code is working:

attach, _ := srv.Users.Messages.Attachments.Get(user, messageid, attachmentid).Do()
decoded, err := base64.URLEncoding.DecodeString(attach.Data)
fileout, err := os.OpenFile(...

That said, I saw the full mode (default) is easier to handle
在GMail API中获取附件

huangapple
  • 本文由 发表于 2016年1月20日 18:54:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/34898052.html
匿名

发表评论

匿名网友

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

确定