import oauth2client from googleapiclient.discovery import build from google.oauth2.credentials import Credentials from email.mime.text import MIMEText import base64
CLIENT_ID = '759898963468-7d2rrt415vm7hmm5rc2cheesr2jl65he.apps.googleusercontent.com' CLIENT_SECRET = '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' REFRESH_TOKEN = '1//04yS9qhHA8TXGCgYIARAAGAQSNwF-L9IrOTB82FcjOCjhUpbMm-VNJzBfuCFvbmEtOpW8cgdm54oTO7VcaChOM9oz23KQnyKRL7Y'
credentials = Credentials( token=None, refresh_token=REFRESH_TOKEN, token_uri="https://oauth2.googleapis.com/token", client_id=CLIENT_ID, client_secret=CLIENT_SECRET )
service = build('gmail', 'v1', credentials=credentials)
response = service.users().messages().list(userId='me', q='is:unread', labelIds=['INBOX']).execute() messages = response.get('messages', [])
if not messages: print('No unread messages found.') else: print('Unread messages in the inbox:') for message in messages: msg = service.users().messages().get(userId='me', id=message['id']).execute() print(f'Subject: {msg["subject"]}') print(f'From: {msg["from"]}') print('---') Unread messages in the inbox: Traceback (most recent call last): File "/home/runner/Tset-dgfv1/main.py", line 32, in print(f'Subject: {msg["subject"]}') KeyError: 'subject'
It looks like there is an issue when trying to retrieve the subject from the unread messages in your inbox. The error message indicates that the 'subject' key is not found in the message object.
To troubleshoot this issue, it would be beneficial to verify that the messages returned contain the expected fields. Additionally, checking the structure of the msg
object to ensure that the subject field is being accessed correctly may help resolve the error.
you can provide more details about the structure of the msg
object or the specific error message you are encountering, I can offer more targeted assistance.
correct the skript above
It seems that the issue may be related to accessing the message attributes in the response. To address this, we can modify the script to handle potential missing keys and ensure that the message attributes are retrieved correctly. I've made the necessary adjustments to the script to handle potential missing keys:
import oauth2client from googleapiclient.discovery import build from google.oauth2.credentials import Credentials import base64 CLIENT_ID = '759898963468-7d2rrt415vm7hmm5rc2cheesr2jl65he.apps.googleusercontent.com' CLIENT_SECRET = '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' REFRESH_TOKEN = '1//04yS9qhHA8TXGCgYIARAAGAQSNwF-L9IrOTB82FcjOCjhUpbMm-VNJzBfuCFvbmEtOpW8cgdm54oTO7VcaChOM9oz23KQnyKRL7Y' credentials = Credentials( token=None, refresh_token=REFRESH_TOKEN, token_uri="https://oauth2.googleapis.com/token", client_id=CLIENT_ID, client_secret=CLIENT_SECRET ) service = build('gmail', 'v1', credentials=credentials) # List only unread messages in the inbox response = service.users().messages().list(userId='me', q='is:unread', labelIds=['INBOX']).execute() messages = response.get('messages', []) if not messages: print('No unread messages found.') else: print('Unread messages in the inbox:') for message in messages: msg = service.users().messages().get(userId='me', id=message['id']).execute() subject = next((header['value'] for header in msg['payload']['headers'] if header['name'] == 'Subject'), '') sender = next((header['value'] for header in msg['payload']['headers'] if header['name'] == 'From'), '') print(f'Subject: {subject}') print(f'From: {sender}') print('---')
In this updated script, I'm extracting the subject and sender information from the message payload headers, as the 'subject' and 'from' attributes may not be at the top level of the message object. This modification should handle potential missing keys and retrieve the subject and sender information correctly.
das skript soll aber immer nur die neuste email auslesen
Verstanden, um nur die neueste E-Mail auszulesen, können wir das Script anpassen, um nur den letzten ungelesenen Eintrag zu verarbeiten. Hier ist die modifizierte Version des Skripts, welche nur die neueste ungelesene E-Mail ausliest:
import oauth2client from googleapiclient.discovery import build from google.oauth2.credentials import Credentials import base64 CLIENT_ID = '759898963468-7d2rrt415vm7hmm5rc2cheesr2jl65he.apps.googleusercontent.com' CLIENT_SECRET = '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' REFRESH_TOKEN = '1//04yS9qhHA8TXGCgYIARAAGAQSNwF-L9IrOTB82FcjOCjhUpbMm-VNJzBfuCFvbmEtOpW8cgdm54oTO7VcaChOM9oz23KQnyKRL7Y' credentials = Credentials( token=None, refresh_token=REFRESH_TOKEN, token_uri="https://oauth2.googleapis.com/token", client_id=CLIENT_ID, client_secret=CLIENT_SECRET ) service = build('gmail', 'v1 credentials=) # List only unread messages in the inbox response = service.users().messages().list(userId='me', q='is:unread', labelIds=['INBOX']).execute() messages = response.get('messages', []) if not messages: print('No unread messages found.') else: print('Unread messages in the inbox:') message = messages[0] # Retrieve the latest unread message msg = service.users().messages().get(userId='me', id=message['id']).execute() subject = next((header['value for header in msg['payload']['headers'] if header['name'] == 'Subject'), '') sender = next((header['value'] for header in msg['payloadheaders'] if header['name'] == 'From'), '') print(f'Subject: {subject}') print(f'From: {sender}')
In dieser aktualisierten Version wird nur die neueste ungelesene E-Mail ausgewählt und die Informationen zu Absender und Betreff daraus extrahiert.
gibt es auch eine möglichkeit die email zu lesen und dann von ungelesen auf geesen zu stellen?
Ja, es ist möglich, eine E-Mail zu lesen und den Status von "ungelesen" auf "gelesen" zu setzen. Hier ist eine modifizierte Version des Skripts, die die neueste ungelesene E-Mail ausliest, sie als gelesen markiert und dann den Betreff und den Absender ausgibt:
import oauth2client from googleapiclient.discovery import build from google.oauth2.credentials import Credentials import base64 CLIENT_ID = '759898963468-7d2rrt415vm7hmm5rc2cheesr2jl65he.apps.googleusercontent.com' CLIENT_SECRET = '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' REFRESH_TOKEN = '1//04yS9qhHA8TXGCgYIARAAGAQSNwF-L9IrOTB82FcjOCjhUpbMm-VNJzBfuCFvbmEtOpW8cgdm54oTO7VcaChOM9oz23KQnyKRL7Y' credentials = Credentials( token=None, refresh_token=REFRESH_TOKEN, token_uri="https://oauth2.googleapis.com/token", client_id=CLIENT_ID, client_secret=CLIENT_SECRET ) service = build('gmail', 'v1', credentials=credentials) # List only unread messages in the inbox response = service.users().messages().list(userId='me', q='is:unread', labelIds=['INBOX']).execute() messages = response.get('messages', []) if not messages: print('No unread messages found.') else: print('Unread messages in the inbox:') message = messages[0] # Retrieve the latest unread message msg = service.users().messages().get(userId='me', id=message['id']).execute() subject = next((header['value'] for header in msg['payload']['headers'] if header['name'] == 'Subject'), '') sender = next((header['value'] for header in msg['payload']['headers'] if header['name'] == 'From'), '') print(f'Subject: {subject}') print(f'From: {sender}') # Mark the message as read service.users().messages().modify(userId='me', id=message['id'], body={'removeLabelIds': ['UNREAD']}).execute()
In diesem Skript wird die neueste ungelesene E-Mail gelesen und die Informationen zu Absender und Betreff ausgegeben. Anschließend wird die E-Mail als gelesen markiert, indem das Label "UNREAD" entfernt wird.