I'm trying to get my last mail using the apiclient
and oauth2client
librarys (quickstart from here : https://developers.google.com/gmail/api/quickstart/quickstart-python).
I am curently able to retrieve all my last mail, including promotion mail and social network mails (which i don't care).
The script is the following (also if you see some mistake or ways to do it faster, please tell me) :
# ... credentials mechanism identical to the quickstart one
gmail_service = build('gmail', 'v1', http=http
# Get the last mails :
last_mails = gmail_service.users().messages().list(userId="me", labelIds=["INBOX"]).execute()
for mailIds in last_mails["messages"]:
all_infos = gmail_service.users().messages().get(userId="me", id=mailIds["id"]).get(userId="me", id=mailIds["id"]).execute()
abstract = all_infos["snippet"]
# Changing json structure
headers = dict([(x["name"], x["value"]) for x in all_infos["payload"]["headers"]])
dest = headers["Delivered-To"]
source = headers["From"]
title = headers["Subject"]
print "Mail : %s\nFrom : %s\nTo : %s\nAbst : %s\n-------" % (
title, source, dest, abstract
)
So my objective is to remove the promotion and social network mails from the listing. Is there any way to do it ?
While sending request to messages.list along with labelIds = "INBOX", include q=category:primary. I tried in API explorer and able to retrieve emails only from inbox.
Response: 200 OK
- Show headers -
{
"messages": [
{
"id": "14d0078c4b6e7b95",
"threadId": "14d0078c4b6e7b95"
},
{
"id": "14d0057a75894568",
"threadId": "14d0057a75894568"
},
{
"id": "14d004211587e8c0",
"threadId": "14d004211587e8c0"
},
{
"id": "14cfed9aef9e1217",
"threadId": "14cfed9aef9e1217"
},