如何使用Gmail API(Python)检索整个邮件正文 [英] How to retrieve the whole message body using Gmail API (python)

查看:256
本文介绍了如何使用Gmail API(Python)检索整个邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用gmail api提取邮件的整个邮件正文.现在我正在使用代码段",但是我需要整个文本.我搜索后发现,这与有效负载有关,但是我不知道如何.有人可以给我举个例子吗?另外,我正在通过python使用Gmail API.

I want to extract the whole message body of mail using gmail api. Right now i am using 'snippets' but i need the entire text. I searched and found that it's something to do with payload, but i didn't understand how. Can someone show me an example? Also, I am using the Gmail api via python.

推荐答案

与noogui所说的一样,但我发现该代码段不会返回整个正文

same as noogui said but I found that snippet wont return the whole body

当代码段超过200个字符时,您将在有效负载下获取它.body.data 你可以使用payload.body.data

when the snippet exceed 200~ chars you will get it under payload.body.data you can find the whole body using payload.body.data

要注意的是,您正在对其进行base64编码,因此您需要对其进行解码:)

the catch is that you are getting it base64encoded so you need to decode it :)

下面的代码可以解决问题

the following code will do the trick

import base64  
mail = service.users().messages().get(userId=user_id, id=id, format="full").execute()

def parse_msg(msg):
    if msg.get("payload").get("body").get("data"):
        return base64.urlsafe_b64decode(msg.get("payload").get("body").get("data").encode("ASCII")).decode("utf-8")
    return msg.get("snippet") 

这篇关于如何使用Gmail API(Python)检索整个邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆