my script includes this line:
encoded = "Basic " + s.encode("base64").rstrip()
But gives me back the error:
LookupError: 'base64' is not a text encoding; use codecs.encode() to handle arbitrary codecs
This line seemed to work fine in python 2 but since switching to 3 I get the error
This string codec was removed in Python 3. Use base64
module:
Python 3.6.1 (default, Mar 23 2017, 16:49:06)
>>> import base64
>>> base64.b64encode('whatever')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
>>> base64.b64encode(b'whatever')
b'd2hhdGV2ZXI='
>>>
Don't forget to convert the data to bytes.
Code as follows:
base64.urlsafe_b64encode('Some String'.encode('UTF-8')).decode('ascii')
For example: return {'raw': base64.urlsafe_b64encode(message.as_string().encode('UTF-8')).decode('ascii')}
Worked for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With