Typer Error : Unicode-objects must be encoded before hashing

Typer Error : Unicode-objects must be encoded before hashing

我想使用 kaufland.de 网站上的 RestAPI。我按照说明签署了来自 : this website .

的请求

这是代码,他们以我为例 运行 通过 SHA-256 HMAC 以 base64 编码签署请求:

import hmac
import hashlib
import time

def sign_request(method, uri, body, timestamp, secret_key):
    plain_text = "\n".join([method, uri, body, str(timestamp)])

    digest_maker = hmac.new(secret_key, '', hashlib.sha256)
    digest_maker.update(plain_text)
    return digest_maker.hexdigest()

method = "POST"
uri = "https://www.kaufland.de/api/v1/units/"
body = ""
timestamp = time.time()
secret_key = "83cfe0909f4e57f05dd403"

print(sign_request(method, uri, body, timestamp, secret_key))

但是上面的代码抛出了一个错误:

TypeError: key: expected bytes or bytearray, but got 'str'

我从找到了解决办法,在secret_key前面加了b :

secret_key = b'83cfe0909f4e57f05dd403'

然而,当我 运行 :

时它仍然抛出错误
TypeError: Unicode-objects must be encoded before hashing

所以我按照 的解决方案导入了 base64 包:

import hmac
import hashlib
import time
import base64

def sign_request(method, uri, body, timestamp, secret_key):
    plain_text = "\n".join([method, uri, body, str(timestamp)])

    digest_maker = hmac.new(secret_key, '', hashlib.sha256)
    digest_maker.update(plain_text)
    return base64.b64encode(digest_maker.hexdigest())

method = "POST"
uri = "https://www.kaufland.de/api/v1/units/"
body = ""
timestamp = time.time()
secret_key = b"a7d0cb1da1ddbc86c96ee5fedd341b7d8ebfbb2f5c83cfe0909f4e57f05dd403"

print(sign_request(method, uri, body, timestamp, secret_key))

但它仍然抛出错误:

Traceback (most recent call last):

  File "<ipython-input-54-83e727ea1edf>", line 22, in <module>
    print(sign_request(method, uri, body, timestamp, secret_key))

  File "<ipython-input-54-83e727ea1edf>", line 10, in sign_request
    digest_maker = hmac.new(secret_key, '', hashlib.sha256)

  File "C:\ProgramData\Anaconda3\lib\hmac.py", line 153, in new
    return HMAC(key, msg, digestmod)

  File "C:\ProgramData\Anaconda3\lib\hmac.py", line 88, in __init__
    self.update(msg)

  File "C:\ProgramData\Anaconda3\lib\hmac.py", line 96, in update
    self.inner.update(msg)

TypeError: Unicode-objects must be encoded before hashing

在这种情况下有人可以帮助我吗?

尝试在分配时或在 hmac.new 中对您的 secret_key 参数进行编码。

示例:

secret_key = "83cfe0909f4e57f05dd403".encode('utf-8')

或者像我上面写的那样:

digest_maker = hmac.new(secret_key.encode('utf-8'), '', hashlib.sha256)

更新:

import hmac
import hashlib
import time

def sign_request(method, uri, body, timestamp, secret_key):
    plain_text = "\n".join([method, uri, body, str(timestamp)])

    digest_maker = hmac.new(secret_key.encode('utf-8'), msg=''.encode('utf-8'), digestmod=digestmod)
    digest_maker.update(plain_text.encode('utf-8'))
    return digest_maker.hexdigest()

method = "POST"
uri = "https://www.kaufland.de/api/v1/units/"
body = ""
timestamp = time.time()
secret_key = "a7d0cb1da1ddbc86c96ee5fedd341b7d8ebfbb2f5c83cfe0909f4e57f05dd403"
digestmod = hashlib.sha256

print(sign_request(method, uri, body, timestamp, secret_key))