问题
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