【发布时间】:2021-06-29 17:40:56
【问题描述】:
在过去的一个月里,我一直在尝试了解 coinlist API (https://coinlist.co),因为没有可用的 API 包装器,这是一项艰巨的任务。我发现他们的 API 文档与 coinbase 交易所非常相似,并尝试推断但没有成功。
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
auth = CoinlistWalletAuth(API_KEY, API_SECRET)
#Test1 - Fetching account balance
response = requests.get('https://trade-api.coinlist.co/v1/accounts', auth=auth)
我得到这个 TypeError: key: expected bytes or bytearray, but got 'str' 在调用响应时。
文档说 - 您必须对签名进行 base64 编码(sha256 HMAC 的输出)。为什么会失败?
【问题讨论】:
标签: python cryptography exchange-server coinbase-api