What is the encoding of the body of Gmail message? How to decode it?
<p>I am using the Python API for Gmail. I am querying for some messages and retrieving them correctly, but the body of the messages looks like total nonsense, even when the MIME type it's said to be <code>text/plain</code> or <code>text/html</code>.</p>

<p>I have been searching all over the API docs, but they keep saying it's a string, when it obviously must be some encoding... I thought it could be <code>base64</code> encoding, but trying to decode it with Python <code>base64</code> gives me <code>TypeError: Incorrect padding</code>, so either it's not <code>base64</code> or I'm decoding badly.</p>

<p>I'd love to provide a good example, but since I'm handling sensitive information I'll have to obfuscate it a bit...</p>

<pre><code>{
 "payload": {
  "mimeType": "multipart/mixed",
  "filename": "",
  "headers": [
   ...
  ],
  "body": {
   "size": 0
  },
  "parts": [
   {
    "mimeType": "multipart/alternative",
    "filename": "",
    "headers": [
     {
      "name": "Content-Type",
      "value": "multipart/alternative; boundary=001a1140b160adc309053bd7ec57"
     }
    ],
    "body": {
    "size": 0
    },
    "parts": [
     {
      "partId": "0.0",
      "mimeType": "text/plain",
      "filename": "",
      "headers": [
       {
        "name": "Content-Type",
        "value": "text/plain; charset=UTF-8"
       },
       {
        "name": "Content-Transfer-Encoding",
        "value": "quoted-printable"
       }
      ],
      "body": {
           "size": 4067,
           "data": "LS0tLS0tLS0tLSBGb3J3YXJkZWQgbWVzc2FnZSAtLS0tLS0tLS0tDQpGcm9tOiBMaW5rZWRJbiA8am9iLWFwcHNAbGlua2VkaW4uY29tPg0KRGF0ZTogU2F0LCBTZXAgMywgMjAxNiBhdCA5OjMwIEFNDQpTdWJqZWN0OiBBcHBsaWNhdGlvbiBmb3IgU2VuaW9yIEJhY2tlbmQgRGV2ZWxvcG..."
      }
</code></pre>

<p>The field that I'm talking about is <code>payload.parts[0].parts[0].body.data</code>. I have truncated it at a random point, so I doubt is decodable like that, but you get the point... What is that encoding?</p>

<p>Also, wouldn't hurt to know where in the docs they explicitly say its base64 (unless it's the standard encoding for MIME?).</p>

<p><strong>UPDATE:</strong> So in the end there was some bad luck involved. I have 5 mails like this, and turns out that the first one is malformed, for some unknown reason. After moving on to the other ones, I am able to decode all of them with the suggested approaches in the answers. Thank you all!</p>

<p>It's base64. You can use base64.decodestring to read it.
The part of the message that your attached is:  '---------- Forwarded message ----------\r\nFrom: LinkedIn &lt;job-apps@linkedin.com&gt;\r\nDate: Sat, Sep 3, 2016 at 9:30 AM\r\nSubject: Application for Senior Backend Develo'</p>

<p>The incorrect padding error means that you're decoding an incorrect number of characters. You're probably trying to decode a truncated message.</p>

python encoding gmail-api