2

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

Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
Francesco Gualazzi
  • 919
  • 1
  • 10
  • 23
  • Which go package are you using to connect connect to Gmail? – booyaa Jan 20 '16 at 12:12
  • "google.golang.org/api/gmail/v1" – Francesco Gualazzi Jan 20 '16 at 13:09
  • you probably want to set the get parameter `format` to `raw` so you get base64: https://developers.google.com/gmail/api/v1/reference/users/messages/get it looks like there's a method you need to use before call Do. https://github.com/google/google-api-go-client/blob/master/gmail/v1/gmail-gen.go#L3393 – booyaa Jan 20 '16 at 14:34
  • Seeing this one http://stackoverflow.com/questions/24745006/gmail-api-parse-message-content-base64-decoding-with-javascript and trying your idea, then let you know – Francesco Gualazzi Jan 20 '16 at 15:39
  • Same goes with raw input...the base64 decode fails – Francesco Gualazzi Jan 20 '16 at 15:55

1 Answers1

5

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 :)

Francesco Gualazzi
  • 919
  • 1
  • 10
  • 23