Gmail API Error from Code Sample - a bytes-like object is required, not 'str'

混江龙づ霸主 提交于 2019-12-04 22:35:53

Found a solution, replace this line:

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

with:

return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}

Notice added .encode() and .decode() method calls.

First, str object is encoded to bytes object - base64.urlsafe_b64encode requires it in Python 3 (compared to str object in Python 2).

Then, the base64 encoded bytes object must be decoded back to str. This is needed as googleapiclient library will attempt to json serialize it later in code and that is not possible for bytes objects.

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