79627194

Date: 2025-05-18 06:25:30
Score: 0.5
Natty:
Report link

I think you should use label['id'] rather than label['name']. Sometimes, these two have the same value; sometimes, they don't.

Ref: https://developers.google.com/workspace/gmail/api/reference/rest/v1/users.messages/list#http-request

for label in labels:
    if label['type'] == 'user':
        label_id = label['id']  # Use the label ID, not the name
        results = service.users().messages().list(
            userId='me',
            labelIds=[label_id],  # This works!
            maxResults=2
        ).execute()

        # Do something with results here
        print(f"Messages with label {label['name']}:", results.get('messages', []))
        break  # Remove break if you want to process all labels
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Raymond