Coinbase Pro发布请求-JSON格式错误



我目前正在开发一款使用Coinbase Pro自动交易的软件。我正在使用请求库,并且具有适用于";获取";请求但失败";POST";s.我想知道是否有人能帮我了解发生了什么。

这是我目前使用的代码:

import time
import hmac
import hashlib
import base64
import requests
from requests.auth import AuthBase
class CoinbaseExchangeAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
message = message.encode('utf-8')
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message, hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest())
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

当与以下内容一起使用时:

api_url = "https://api.pro.coinbase.com/"
auth = CoinbaseExchangeAuth("*****", "*****", "*****")
request = requests.get(api_url + "accounts", auth=auth).json()

它工作得很好。但我一尝试:

order = {'size': '0.0001', 'price': '100', 'side': 'sell', 'product_id': 'BTC-EUR'}
request = requests.post(api_url + "orders", data=order, auth=auth)
print(request.json())

我得到{'message': 'malformed json'}。我认为这与(request.body or '')有关,但我找不到修复方法。

感谢任何人的帮助!

最终,问题是request = requests.post(api_url + "orders", data=order, auth=auth)-request = requests.post(api_url + "orders", data=json.dumps(order), auth=auth)解决了问题

最新更新