Bytes message argument error

前端 未结 3 1656
我寻月下人不归
我寻月下人不归 2021-01-18 01:25

I can\'t figure out what the \'bytes\' method is complaining about. In the code below, i am trying to generate an authentication key for my client and i keep getting this er

相关标签:
3条回答
  • 2021-01-18 02:01

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

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

    0 讨论(0)
  • 2021-01-18 02:11

    The likely reason you encountered this problem is the code you were using was written for Python 3.x and you executed it under Python 2.x.

    I know someone has already partially stated this, but I thought it might be helpful to make it clearer to people new to Python who may not realize why the 'utf-8' argument was being used as the person asking the question noted that they did not know what the argument was for.

    Anyone who comes here may find that useful in understanding why there was an argument of 'utf-8'.

    0 讨论(0)
  • 2021-01-18 02:12

    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)
    
    0 讨论(0)
提交回复
热议问题