处理类似问题的问题:SO 1 https://stackoverflow.com/questions/7585307/typeerror-unicode-objects-must-be-encoded-before-hashing, SO 2 https://stackoverflow.com/questions/13265439/python-3-3-unicode-objects-must-be-encoded-before-hashing, SO 3 https://stackoverflow.com/questions/37640868/python-3-4-error-unicode-objects-must-be-encoded-before-hashing.
我已经尝试过他们的答案,将几乎任何字符串编码为utf-8
, but hmac
仍然告诉我对我的 unicode 字符进行编码。最大的问题是我什至无法识别出有问题的变量;打印输出告诉我它们是strings
or bytes
;对于前者,我附上.encode()
,但这并没有帮助。
我正在尝试查询 GDAX API 并且我也使用 API 页面上给出的代码 https://docs.gdax.com/?python#generating-an-api-key。由于是为 Python2.7 编写的,我认为可能存在编码等问题,但这对我来说没有意义。
My code:
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
object.
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')
namely:
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对象,但我以后也不能有字节?我到底该如何解决这个问题?感谢对此的任何帮助,因为我非常困惑。