How to solve “a bytes-like object is required, not 'str'” in create_message() function?

怎甘沉沦 提交于 2021-01-29 08:34:18

问题


I'm getting an error in creating a new message using create_message(). function listed over https://developers.google.com/gmail/api/guides/drafts.

def create_message(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message['to'] = to
    message['from'] = sender
    message['subject'] = subject
    return {'raw': base64.urlsafe_b64encode(message.as_string())}

Error: TypeError: a bytes-like object is required, not 'str'


回答1:


base64.urlsafe_b64encode expects bytes, but the type of message.as_string() is str.

Use the message's as_bytes method instead.

return {'raw': base64.urlsafe_b64encode(message.as_bytes())}


来源:https://stackoverflow.com/questions/55411408/how-to-solve-a-bytes-like-object-is-required-not-str-in-create-message-fu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!