I am using Gmail API to access my gmail data and google python api client.
According to documentation to get the message attachment they gave one sample for python
https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get
but the same code i tried then i am getting error:
AttributeError: 'Resource' object has no attribute 'user'
line where i am getting error:
message = service.user().messages().get(userId=user_id, id=msg_id).execute()
So i tried users()
by replacing user()
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
but i am not getting part['body']['data']
in for part in message['payload']['parts']
i tested codes above and doesn't worked. And i updated some stuff for other posts. WriteFileError
You can still miss attachments by following @Ilya V. Schurov or @Cam T answers, the reason is because the email structure can be different based on the
mimeType
.Inspired by this answer, here is my approach to the problem.
Expanding @Eric answer, I wrote the following corrected version of GetAttachments function from the docs:
It's definitely "users()". The format of the response Message is largely dependent on the format parameter you use. If you use the default (FULL) then parts will either have part['body']['data'] or, when data is large, with an "attachment_id" field that you can pass to messages().attachments().get().
If you look at the attachments docs you'll see this: https://developers.google.com/gmail/api/v1/reference/users/messages/attachments
(Would be nice if this was also mentioned on the main messages docs page also.)
I made the following changes for the code above and works totally fine for every email id contains attachment documents, I hope this can help because with the API example you will get an error Key.
Google Official API for Attachments