Really stuck on this one and pulling my hair out trying to figure out what to do, searched everywhere and tried everything but still stuck. Any help is much appreciated.
From Google's gmail rest api (sending messages):
"The entire email message in an RFC 2822 formatted and base64url encoded string. Returned in messages.get and drafts.get responses when the format=RAW parameter is supplied."
base64url encoding is not the same as base64 encoding.
in python, the base64 library offers b64encode() and urlsafe_b64encode(). If you don't use urlsafe_b64encode(), you will get that error, not all the time so it may pass a simple unit test, but it will fail soon enough.