I'm trying to export a csv-file from the Gmail-API via the Users.messages.attachments: get method (1) and convert it to a pandas data-frame. I already have the ID of the attachment and the ID of the message and everything is working fine. The documentation says that the body data of the attachment is "part as a base64url encoded string" (2) and now i would like to convert the the csv-file to a pandas data-frame. Following this post I tried the following:
[...]
#get the attachment
file = service.users().messages().attachments().get(userId='me', messageId=message_id, id=attachmentId).execute()
#convert the file to a pandas data-frame.
data = file['data']
str_csv = base64.urlsafe_b64decode(data)
df = pd.read_csv(StringIO(str_csv))
This is what print(data)
looks like :
__5CAGUAcgBpAGMAaAB0ACAAegB1ACAAQQB1AGsAdABpAG8AbgBzAGQAYQB0AGUAbgAgAGYA_AByACAASwBhAG0AcABhAGcAbgBlAG4ACgAxAC4AIABKAGEAbgB1AGEAcgAgADIAMAAyADAAIAAtACAAMwAxAC4AIABKAGEAbgB1AGEAcgAgADIAMAAyADAACgBHAGUAcgDkAHQACQBEAG8AbQBhAGkAbgAgAGQAZQByACAAYQBuAGcAZQB6AGUAaQBnAHQAZQBuACAAVQBSAEwACQBBAG4AdABlAGkAbAAgAGEAbgAgAG0A9gBnAGwAaQBjAGgAZQBuACAASQBtAHAAcgBlAHMAcwBpAG8AbgBlAG4ACQDcAGIAZQByAHMAYwBoAG4AZQBpAGQAdQBuAGcAcwByAGEAdABlAAkAUgBhAHQAZQAgAGQAZQBy
and so on...
Unfortunately I get the following error-message:
Does anyone have an idea how I can fix this or can explain why it isn't working?
(1) see https://developers.google.com/gmail/api/v1/reference/users/messages/attachments/get
(2) see https://developers.google.com/gmail/api/v1/reference/users/messages/attachments