Coinlist exchange API身份验证-Python

Coinlist exchange API身份验证-Python,python,cryptography,exchange-server,coinbase-api,Python,Cryptography,Exchange Server,Coinbase Api,在过去的一个月里,我一直试图理解coinlistapi(),因为没有可用的API包装,这是一项艰巨的任务。我发现他们的API文档与coinbase exchange非常相似,并尝试进行推断,但没有成功 import json, hmac, hashlib, time, requests from requests.auth import AuthBase # Before implementation, set environmental variables with the names AP

在过去的一个月里,我一直试图理解coinlistapi(),因为没有可用的API包装,这是一项艰巨的任务。我发现他们的API文档与coinbase exchange非常相似,并尝试进行推断,但没有成功

import json, hmac, hashlib, time, requests
from requests.auth import AuthBase

# Before implementation, set environmental variables with the names API_KEY and API_SECRET
API_KEY = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
API_SECRET = 'xxxx/xxxxxxxxxxxxxxxxxxxxx/xxxxxxxxxxxxxxxxx=='

# Create custom authentication for Coinlist API
class CoinlistWalletAuth(AuthBase):
    def __init__(self, api_key, secret_key):
        self.api_key = api_key
        self.secret_key = secret_key

    def __call__(self, request):
        timestamp = str(int(time.time()))
        message = timestamp + request.method + request.path_url + (request.body or '')
        signature = hmac.new(self.secret_key, message, hashlib.sha256).hexdigest()

        request.headers.update({
            'CL-ACCESS-SIGN': signature,
            'CL-ACCESS-TIMESTAMP': timestamp,
            'CL-ACCESS-KEY': self.api_key,
        })
        return request
我得到了这个TypeError:key:应该是bytes或bytearray,但是在调用响应时得到了'str'

文档说-您必须对签名(sha256 HMAC的输出)进行base64编码。为什么会失败?

有两件事:

hmac.new(key,msg=None,digestmod='')

返回一个新的hmac对象。key是一个字节或bytearray对象,提供密钥。(……)

auth = CoinlistWalletAuth(API_KEY, API_SECRET)
#Test1 - Fetching account balance
response = requests.get('https://trade-api.coinlist.co/v1/accounts', auth=auth)
import base64
...
secret_key_as_bytes = base64.b64decode(self.secret_key, altchars=None)
digest = hmac.new(secret_key_as_bytes, message, hashlib.sha256).digest()
signature = str(base64.b64encode(digest))