Я получаю данные вложения из Gmail, в то время как я пишу данные, они выдают ошибку
UnicodeDecodeError 'utf-8' кодек не может декодировать байт 0xf0 в позиции 14: недействительнобайт продолжения
service = build('gmail', 'v1', credentials=creds)
results = service.users().messages().list(userId='me',labelIds = ['INBOX'],maxResults=10).execute()
messages = results.get('messages', [])
if not messages:
print("No messages found.")
else:
print("Message snippets:")
count = 0
if os.path.exists('token.pickle'):
for message in messages:
msg_dict = {}
msgbody_dict = {}
msg = service.users().messages().get(userId='me', id=message['id']).execute()
ID=message['id']
payld = msg['payload']
headr = payld['headers']
payload_data = payld['body']
#print('type========================',payld)
if 'parts' in payld.keys():
payload_parts = msg['payload']['parts']
for i in payload_parts:
payload_headers = i['headers']
for NAME in payload_headers:
if NAME['name'] == 'Content-Type':
content_type = NAME['value']
if 'text/html' in content_type or 'text/plain' in content_type:
partstype_dat = i['body']['data']
partstype_dat=partstype_dat.replace('-', '+')
partstype_dat=partstype_dat.replace('_', '/')
payldp=base64.urlsafe_b64decode(partstype_dat)
payldp = str(payldp , 'utf-8')
msgbody_dict['data'] = payldp
payload_pldata.append(msgbody_dict)
elif i['filename']:
attach_parts = i['body']
att_id = attach_parts['attachmentId']
att_data = service.users().messages().attachments().get(userId='me', messageId=ID, id=att_id).execute()
encydoc_data = att_data['data']
doc_data = base64.urlsafe_b64decode(encydoc_data.encode('UTF-8'))
dcdata = doc_data.decode('ASCII')
final_docdata = str(doc_data , 'UTF-8')
with open(i['filename'],"w") as f:
f.write(final_docdata)