Coinbase Pro post 请求 - 格式错误 JSON
Coinbase Pro post request - Malformed JSON
我目前正在开发一款使用 Coinbase Pro 进行自动交易的软件。我正在使用请求库并拥有适用于“GET”请求但对“POST”请求无效的代码。我想知道是否有人可以帮助我了解发生了什么。
这是我目前使用的代码:
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)
解决了问题
我目前正在开发一款使用 Coinbase Pro 进行自动交易的软件。我正在使用请求库并拥有适用于“GET”请求但对“POST”请求无效的代码。我想知道是否有人可以帮助我了解发生了什么。
这是我目前使用的代码:
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)
解决了问题