Reputation: 11
I am getting an error when converting base 64 into python, i can't find the solution
uniq_token_hash = hashlib.sha256(uniq_token_string.encode('UTF-8')).hexdigest()
print ('UNIQ HASH: %s' % uniq_token_hash)
auth_token = b64encode('%s;%s;%s' % (devapp,unix_timestamp, uniq_token_hash))
print ('AUTH TOKEN: %s' % auth_token)
line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=False)
TypeError: a bytes-like object is required, not 'str'
Upvotes: 0
Views: 601
Reputation: 308520
There are two kinds of strings in Python. The first is made of Unicode codepoints (characters), and it's called str
. The second is a sequence of bytes (small integers from 0 to 255), and it's called bytes
. b64encode
requires bytes
as input. You already know how to convert str
to bytes
, because you've done it once with .encode
.
auth_token = b64encode(('%s;%s;%s' % (devapp,unix_timestamp, uniq_token_hash)).encode('UTF-8'))
Upvotes: 1
Reputation: 1612
You have to transfer the UTF-8 string to a simple byte array, you can change a string to byte array with this following code
old_string = "string"
byte_array = old.string.encode()
# returns b'string'
new_string = byte_array.decode()
In a recent Python version I would use a f'string. Maybe use something like this:
byte_array = f'{devapp};{unix_timestamp};{uniq_token_hash}'.encode()
auth_token = b64encode(byte_array)
Please bear in mind, that you have to handle encoding carefully and be well aware what do you store how.
Upvotes: 0