What is the encoding of the body of a Gmail message? How to decode it?

I am using the Python API for Gmail. I request some messages and receive them correctly, but the message body looks like complete nonsense, even if the MIME type is specified as text/plainor text/html.

I searched all the API docs, but they keep saying this as a string, when obviously there should be some encoding ... I thought it could be an encoding base64, but trying to decode it using Python base64gives me TypeError: Incorrect padding, so either it is not base64, or I badly decode.

I would like to provide a good example, but since I process confidential information, I will have to confuse it a bit ...

{
 "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..."
      }

, , payload.parts[0].parts[0].body.data. , , , ... ?

, , base64 ( MIME?).

: , , - . 5 , , , . , . !

+4
4

base64.

:

---------- Forwarded message ----------
From: LinkedIn <job-apps@linkedin.com>
Date: Sat, Sep 3, 2016 at 9:30 AM
Subject: Application for Senior Backend Develop

:

3 , , . , , , .

import base64

body = "LS0tLS0tLS0tLSBGb3J3YXJkZWQgbWVzc2FnZSAtLS0tLS0tLS0tDQpGcm9tOiBMaW5rZWRJbiA8am9iLWFwcHNAbGlua2VkaW4uY29tPg0KRGF0ZTogU2F0LCBTZXAgMywgMjAxNiBhdCA5OjMwIEFNDQpTdWJqZWN0OiBBcHBsaWNhdGlvbiBmb3IgU2VuaW9yIEJhY2tlbmQgRGV2ZWxv"

result = base64.b64decode(body)

print(result)

UPDATE

. API gMail:

  message = service.users().messages().get(userId='me', id=msg_id, format='full').execute()
  msg_str = base64.urlsafe_b64decode(message['payload']['body']['data'].encode('UTF8'))
  mime_msg = email.message_from_string(msg_str) 

  print(msg_str)

: https://developers.google.com/gmail/api/v1/reference/users/messages/get#python

+6

base64. base64.decodestring . , : '---------- ----------\r\nFrom: LinkedIn < job-apps@linkedin.com > \r\n: , 3 2016 9:30 . \n\n\n\n\n \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \

, . , .

+2

, - base64 ( "base64url" ). , MessagePartBody : https://developers.google.com/gmail/api/v1/reference/users/messages/attachments

And he says that the type is "bytes" (which, obviously, is not saved for transmission via JSON as-is), but I agree with you, it does not explicitly indicate that base64url, encoded as other "byte" fields, is in the API.

Regarding filling problems, is it because you are truncating? If not, check that "len (data)% 4 == 0", if not, it means that the API returns unstated data, which would be unexpected.

+1
source
>>> "LS0tLS0tLS0tLSBGb3J3YXJkZWQgbWVzc2FnZSAtLS0tLS0tLS0tDQpGcm9tOiBMaW5rZWRJbiA8am9iLWFwcHNAbGlua2VkaW4uY29tPg0KRGF0ZTogU2F0LCBTZXAgMywgMjAxNiBhdCA5OjMwIEFNDQpTdWJqZWN0OiBBcHBsaWNhdGlvbiBmb3IgU2VuaW9yIEJhY2tlbmQgRGV2ZWxvcG==".decode('base64')
'---------- Forwarded message ----------\r\nFrom: LinkedIn <job-apps@linkedin.com>\r\nDate: Sat, Sep 3, 2016 at 9:30 AM\r\nSubject: Application for Senior Backend Develop'
0
source

Source: https://habr.com/ru/post/1653923/


All Articles