gpt4 book ai didi

python - Unicode Objects must be encoded before hashing 错误

转载 作者:太空狗 更新时间:2023-10-30 02:57:14 26 4
gpt4 key购买 nike

处理类似问题的问题:SO 1 , SO 2 , SO 3 .

我已经尝试了他们的答案,将几乎所有字符串编码为 utf-8,但是 hmac 仍然告诉我对我的 unicoe 字符进行编码。最大的问题是我什至无法识别有问题的变量;打印输出告诉我它们是 stringsbytes;对于前者,我附加了 .encode(),但这并没有帮助。

我正在尝试查询 GDAX API,我也是 using the code as given on their API page .由于是为 Python2.7 编写的,我认为编码等方面可能存在问题,但这对我来说毫无意义。

我的代码:

class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key.encode()
self.secret_key = secret_key.encode()
self.passphrase = passphrase.encode()

def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
hmac_key = base64.b64decode(self.secret_key)
#print(hmac_key, type(hmac_key))
#print(message, type(message))
signature = hmac.new(hmac_key, message, hashlib.sha256)

signature_b64 = signature.digest().encode('base64').rstrip('\n')


request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request

错误:

File "F:\git\knowhere\Private\bitex-crawler\gdax_client\gdaxex\api.py", line 47, in __call__
signature = hmac.new(hmac_key, message, hashlib.sha256)
File "C:\Users\nls\Anaconda3\lib\hmac.py", line 144, in new
return HMAC(key, msg, digestmod)
File "C:\Users\nls\Anaconda3\lib\hmac.py", line 84, in __init__
self.update(msg)
File "C:\Users\nls\Anaconda3\lib\hmac.py", line 93, in update
self.inner.update(msg)
TypeError: Unicode-objects must be encoded before hashing

当我键入检查我提供给 hmac.new() 调用的对象时,它告诉我我有一个 str 对象和一个 bytes对象。

print(type(hmac_key)) # <bytes>
print(type(message)) # <str>

当然,我认为我也需要对那个傻瓜进行编码:

signature = hmac.new(hmac_key, message.encode(), hashlib.sha256)

这导致了这一行的错误:

signature_b64 = signature.digest().encode('base64').rstrip('\n')

即:

File "F:/git/knowhere/Private/bitex-crawler/gdax_client/client.py",
[..]
File "F:\git\knowhere\Private\bitex-crawler\gdax_client\gdaxex\api.py", line 123, in _query
r = api_query(url, json=req, auth=auth)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\api.py", line 67, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\api.py", line 53, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\sessions.py", line 454, in request
prep = self.prepare_request(req)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\sessions.py", line 388, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\models.py", line 297, in prepare
self.prepare_auth(auth, url)
File "C:\Users\nls\Anaconda3\lib\site-packages\requests\models.py", line 490, in prepare_auth
r = auth(self)
File "F:\git\knowhere\Private\bitex-crawler\gdax_client\gdaxex\api.py", line 49, in __call__
signature_b64 = signature.digest().encode('base64').rstrip('\n')
AttributeError: 'bytes' object has no attribute 'encode'

..所以我不能有未编码的 unicode 对象,但我以后也不能有字节?我到底该如何解决这个问题?感谢对此的任何帮助,因为我非常困惑。

最佳答案

"Parameter msg can be of any type supported by hashlib." .

"Note: Feeding string objects into is not supported, as hashes work on bytes, not on characters." .

因此,您的消息必须是 bytes 类型。在消息上使用 .encode() 将为您提供字节对象。

注意:这仅对 python 3 是必需的!

要将摘要编码为 base64,请使用 base64 library .

import base64
signature_b64 = base64.b64encode(signature.digest())

关于python - Unicode Objects must be encoded before hashing 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37763235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com