You call users.messages.get, walk payload.parts, find the text/plain part and get something like this:
{ "mimeType": "text/plain", "body": { "size": 46, "data": "SGkgTGVuYSwNCklzIEZyaWRheSBPSyBmb3Igb3JkZXIgIzQ0NzE_IPCfmYINCg==" } } That data field trips people up in three ways: it is Base64URL, not standard Base64; the size next to it counts bytes, not characters; and if you asked for format=RAW instead, you are looking at a different thing that may need a second decode. Here is how to handle all three, with an example you can check by hand. (The message above is synthetic: made-up text, made-up order number, no real mailbox.)
1. data is Base64URL, so plain Base64 decoders choke on it
Google's reference for MessagePartBody describes data as "the body data of a MIME message part as a base64url encoded string" (users.messages.attachments reference, page last updated 24 Mar 2025, read 5 Dec 2026). Base64URL (RFC 4648 §5) uses - and _ where standard Base64 uses + and /.
Our example string has an _ in it (...NzE_IPCf...). Here is what happens when you feed it to common decoders. We ran each of these while writing:
| Decoder | Result |
|---|---|
Browser/Node atob(s) | InvalidCharacterError: Invalid character |
Python base64.b64decode(s) | binascii.Error: ... number of data characters (61) cannot be 1 more than a multiple of 4 |
Python base64.b64decode(s, validate=True) | binascii.Error: Only base64 data is allowed |
Python base64.urlsafe_b64decode(s) | works |
Node Buffer.from(s, "base64url") | works |
The Python error is worth understanding. Without validate=True, b64decode silently throws away characters outside the standard alphabet. It dropped the _, leaving 61 data characters, which can't be valid Base64. If the dropped characters had happened to leave a valid length, you would get wrong bytes and no error at all.
Fix: use the URL-safe decoder, and don't assume the padding is there. Python's urlsafe_b64decode needs = padding, so add it defensively:
import base64 def gmail_b64(s: str) -> bytes: return base64.urlsafe_b64decode(s + "=" * (-len(s) % 4)) -len(s) % 4 is 0 when the length is already a multiple of 4, so this is safe either way.
2. size counts bytes, which makes it a free integrity check
The same reference describes size as the "number of bytes for the message part data (encoding notwithstanding)". In other words: compare it with the length of the decoded bytes, not the Base64 string and not the decoded text.
Worked example (verified)
Input (illustrative, synthetic): the data string above, 64 characters, ending in ==.
- Base64 length check: 64 characters, a multiple of 4. Two
=at the end means the last group carries 1 byte. - Bytes: 64 ÷ 4 × 3 = 48, minus 2 for the two
=, gives 46 bytes. That matches"size": 46. - Text: decoding those 46 bytes as UTF-8 gives
Hi Lena,\r\nIs Friday OK for order #4471? 🙂\r\n. - Characters vs bytes: that text is 43 Unicode code points (44 in JavaScript's
.length, because the emoji is a surrogate pair). The emoji alone is 4 bytes (F0 9F 99 82), which is why the byte count is higher.
We checked this three ways: Python urlsafe_b64decode (46 bytes), Node Buffer.from(s, "base64url") (46 bytes), and the decode function from base64decodetool.com's own base64.worker.js, run in Node. The site's decoder maps -→+ and _→/, pads with = to a multiple of 4, and returned the same 46 bytes and the same text, with or without the trailing ==.
data = "SGkgTGVuYSwNCklzIEZyaWRheSBPSyBmb3Igb3JkZXIgIzQ0NzE_IPCfmYINCg==" raw = gmail_b64(data) assert len(raw) == 46 # matches body.size text = raw.decode("utf-8") If len(raw) doesn't match size, suspect truncation in your own pipeline (a log line cut off, a DB column too short) before suspecting Gmail.
3. The charset is still your job
Base64 gives you bytes. It says nothing about which character set those bytes are in. Read the part's Content-Type header from headers[] (for example text/plain; charset=ISO-8859-1) and decode with that, not with a hard-coded UTF-8.
A quick test: the Latin-1 bytes for Café au lait\r\n encode to Q2Fm6SBhdSBsYWl0DQo. Decoded as UTF-8 you get Caf� au lait. The byte E9 is é in Latin-1 but invalid on its own in UTF-8.
4. format=RAW is a different animal: two layers
With format=RAW, the message resource has a raw field: "the entire email message in an RFC 2822 formatted and base64url encoded string" (users.messages reference, read 5 Dec 2026). Decoding it once gives you the whole message (headers, MIME boundaries and every part) in the form it travels over email. Each part keeps its own Content-Transfer-Encoding.
Synthetic example: a raw string that starts RnJvbTog... decodes to:
From: orders@example.com To: lena@example.org Subject: Order #4471 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Your caf=C3=A9 order #4471 is ready. See the =C3=A9? That's the second layer (quoted-printable). Don't hand-roll it. Let a MIME parser do it:
import email from email import policy msg = email.message_from_bytes(gmail_b64(raw_field), policy=policy.default) print(msg.get_content()) # 'Your café order #4471 is ready.\r\n' A useful habit: if a decoded Gmail payload still shows =3D, =C3=A9, or another Base64 block under a Content-Transfer-Encoding: base64 header, you are holding raw MIME, not a part body.
Spotting which one you have: a raw message starts with a header line. Some header names give a stable Base64 prefix: From: → RnJvbTog, Received: → UmVjZWl2ZWQ6, Delivered-To: → RGVsaXZlcmVkLVRv. An HTML part body often starts PGh0 (<ht) or PCFE (<!D).
5. Attachments: no data, an attachmentId instead
For larger parts, body.data is empty and body.attachmentId is set. You then call messages.attachments.get, and the response is another MessagePartBody whose data is again Base64URL. Use the same gmail_b64() and the same size check, then write the bytes to disk. Never .decode() a PDF or image as text.
How to use the tool for a quick manual check
- Open the URL-Safe Base64 tool. It opens on Decode Base64, with URL-Safe Format already switched on and Live Conversion enabled.
- Paste the
datavalue into the input box.-,_and missing=are all fine. - Keep Character Set: UTF-8 for text parts. If you see "Output contains replacement characters", switch to Hexadecimal to look at the raw bytes (that's how you spot the Latin-1
E9above). - Use Copy or Download (.txt) to save the output. The tool runs in your browser, but still don't paste real customer mail or secrets into any web page.
Summary
-
body.dataandraware Base64URL: useurlsafe_b64decode/Buffer.from(s, "base64url"), and pad defensively. -
body.sizeis a byte count: compare it with the decoded bytes. - Decode text with the part's declared charset.
-
format=RAWis raw MIME after one decode. Parse it, don't regex it. - Base64 is an encoding, not encryption, and decoding proves nothing about who sent the mail.
Tool: https://base64decodetool.com/url-safe/
Disclosure: this tool is from Mind vs Drive, the team behind this blog.
