简体   繁体   English

base64 编码不接受 mime 消息

[英]base64 encoding not taking mime message

I am trying to send an oauth gmail using python and am not able to create MimeMessages that agree with Google's API.我正在尝试使用 python 发送 oauth gmail,但无法创建与 Google API 一致的 MimeMessages。 After creating a sample message, I use base64 to encode it as a string.创建示例消息后,我使用 base64 将其编码为字符串。 However, I come up with the error: TypeError: a bytes-like object is required, not 'str'但是,我想出了错误: TypeError: a bytes-like object is required, not 'str'

The line at the top of the stack:堆栈顶部的行:

return {'raw': base64.urlsafe_b64encode(message_str)}

I've tried using different versions of encoding ( encoders.encode_base64(message) , message.as_string().encode("utf-8") , etc.) and have tried converting the message.as_string() to bytes (as the error message suggests) but am met with different error messages from Google, saying the encoding doesn't meet their requirements, which are "MIME email messages compliant with RFC 2822 and encoded as base64url strings."我尝试使用不同版本的编码( encoders.encode_base64(message)message.as_string().encode("utf-8")等)并尝试将 message.as_string() 转换为字节(作为错误消息建议)但我遇到了来自 Google 的不同错误消息,称编码不符合他们的要求,即“符合 RFC 2822 并编码为 base64url 字符串的 MIME 电子邮件消息”。

My entire function is as follows.我的整个功能如下。

def create_message(sender, to, subject, message_text):

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

I have no idea why this shouldn't work.我不知道为什么这不起作用。 It is copy-pasted from the tutorial.它是从教程中复制粘贴的。 I am running python 3.7.2我正在运行 python 3.7.2

For anyone who has this problem later, this seemed to work对于以后遇到此问题的任何人,这似乎都有效

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

based on the answer here , you could use:根据这里的答案,您可以使用:

'string'.as_bytes() '字符串'.as_bytes()

Not sure why gmail api docs have this error in their code but this is how I got it to work.不知道为什么gmail api 文档在他们的代码中有这个错误,但这就是我让它工作的方式。 (possibly, they are referring to python 2) (可能,他们指的是python 2)


To put this answer in context of your specific question, I did this:为了将此答案放在您的具体问题的上下文中,我这样做了:

def create_message(sender, to, subject, message_text):

    message = MIMEText(message_text)
    message['To'] = to
    message['From'] = sender
    message['Subject'] = subject
    message_bytes = message.as_bytes()
    return {'raw': base64.urlsafe_b64encode(message_bytes).decode('ascii')}

I used decode('ascii') here because the result from this will need to be a json string and bytes cannot be serialized.我在这里使用了decode('ascii')因为它的结果需要是一个 json 字符串并且字节不能被序列化。 You are likely to get an error such as TypeError: Object of type bytes is not JSON serializable otherwise.您可能会收到诸如TypeError: Object of type bytes is not JSON serializable的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM