File size: 765 Bytes
388c73a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import base64
from googleapiclient.discovery import build
from google.oauth2 import service_account

def get_latest_email():
    # Replace with your own Gmail service account setup
    creds = service_account.Credentials.from_service_account_file(
        'credentials.json',
        scopes=['https://www.googleapis.com/auth/gmail.readonly']
    )
    service = build('gmail', 'v1', credentials=creds)
    results = service.users().messages().list(userId='me', labelIds=['INBOX'], maxResults=1).execute()
    msg = results['messages'][0]
    msg_data = service.users().messages().get(userId='me', id=msg['id']).execute()
    payload = msg_data['payload']
    data = payload['body']['data']
    return base64.urlsafe_b64decode(data).decode("utf-8")