Byte Message Error

I cannot understand what the "byte" method is complaining about. In the code below, I'm trying to create an authentication key for my client, and I keep getting this error [1]

import hmac import hashlib import base64 message = bytes("Message", 'utf-8') # errors here secret = bytes("secret", 'utf-8') signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()); print(signature) 

[one]

 Traceback (most recent call last): File "API/test/auth-client.py", line 11, in <module> message = bytes("Message", 'utf-8') TypeError: str() takes at most 1 argument (2 given) 
+8
python base64
source share
3 answers

bytes() in Python 2.x is the same as str() , and it takes only one string argument.

Use only message = "Message" and secret = "secret" . You don't even need bytes() here.

+6
source share

The likely reason you ran into this problem is because the code you used was written for Python 3.x and you ran it under Python 2.x.

I know that someone already partially said this, but I thought it would be useful to make it clearer for people new to Python who might not understand why the argument “utf-8” was used as the person asking the question that they didn’t know what the arguments were for.

Anyone who comes here can find this helpful in understanding why there was a "utf-8" argument.

+6
source share

to try

 import hmac import hashlib import base64 message = bytes("Message") secret = bytes("secret") signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()) print(signature) 
+1
source share

All Articles