July 20, 2020

gmail api message body (full text)

#  python
#  by krutmaster
def getText(service, userId, msg_id):
    message = gmail_service.users().messages().get(userId='me', id=msg_id, format='full').execute()
    text = message.get('payload').get('parts')[0].get('body').get('data')
    
    if text:
            return base64.urlsafe_b64decode(text.encode('ASCII')).decode('utf-8')
    
    return message['snippet']

Hi, Gmail api have many problems, one of this — get text from message. In doc incorrect function, I write own.

If text in message more 200 charset, he will wait in payload - parts - 0 element list - body - data. Else — in snippet.

Youtube video, where I explain in more detail (but only Russian language) — link, and my github. Goodbye!