Resolve Gamail API mail content data decode error

MorrisLin
2 min readMay 30, 2021
Gmail icon from https://zh.wikipedia.org/wiki/Gmail

Recently I try to develop a service with Gmail API / GCP PubSub / LINE Notify. I use GCP PubSub to get notifications from Gmail when it receives mail, push to my server deployed on Heroku’s Django server, and use Gmail API to get unread mail with specific labels and then get mail content and subject. However, LINE Notify to send to a particular group.

In test mail, it’s success decode to UTF-8, but when I deployed to prod server, using Gmail is received many emails from many 3rd services, so in prod, test discovered some mail couldn’t decode success, the error is

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0–1: ordinal not in range(128)

and my code is

base64.urlsafe_b64decode(text.encode("ASCII")).decode("utf-8")

So I being start to find out why will error, and I remove text.encode(“ASCII”)

and it’s have another error,

TypeError:Incorrect padding

So that is root cause, it’s seen Gmail’s mail encode content have some missing, so that can’t be decode by base64, I fix with little change, and here is my resolve way.

missing_padding = 4 - len(text) % 4if missing_padding:    text += "=" * missing_paddingreturn base64.urlsafe_b64decode(text).decode("utf-8")

Using missing_padding to calculate how many in missing, and append same missing work with = and it looks work for me.

reference: https://www.itread01.com/content/1529332812.html

--

--

MorrisLin

Back-end engineer turn into Blockchain software engineer