Kucoin Api v2 Python 下新订单码 - {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}

Kucoin Api v2 Python Place a New Order Code - {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}

出现这个错误如何解决?我正在尝试创建一个函数来下订单 kucoin api v2 python 但给出错误如何修复

我试着用完整的代码来实现这个功能,但仍然找不到解决办法,到底出了什么问题

Kucoin Api v2 Python Place a New Order Code - {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}
base_uri = 'https://api.kucoin.com'

def float_to_dic(price):
    formatted_float = "{:.9f}".format(price)
    return formatted_float

def get_headers(method, endpoint):
    now = int(time.time() * 1000)
    str_to_sign = str(now) + method + endpoint
    signature = base64.b64encode(hmac.new(api_secret.encode(), str_to_sign.encode(), hashlib.sha256).digest()).decode()
    passphrase = base64.b64encode(hmac.new(api_secret.encode(), api_passphrase.encode(), hashlib.sha256).digest()).decode()
    return {'KC-API-KEY': api_key,
            'KC-API-KEY-VERSION': '2',
            'KC-API-PASSPHRASE': passphrase,
            'KC-API-SIGN': signature,
            'KC-API-TIMESTAMP': str(now)
    }

#List Accounts
method = 'POST'
endpoint = '/api/v1/orders'

price_buy = float(1900)
quantity = 0.3

price = "{:.9f}".format(price_buy)
symbol = 'ETH-USDT'


body = '{"text":"t-123456","symbol":"' + symbol + '","type":"market","tradeType":"TRADE","side":"buy","time_in_force":"gtc","auto_borrow":false}'

response = requests.request(method, base_uri+endpoint, headers=get_headers(method, endpoint), data=body)


print(response.status_code)
print(response.json())

您需要为您的签名编码过程传递您的时间戳

我做的另一件事是:

encode(Config.SECRET_KEY, timestamp + "POST" + "/api/v1/orders" + kucoinOrderRequest.toString()) + "",

KucoinOrderRequest模型class toString函数基本上是一个json格式的请求,像这样:

"{" +
            "\n\"clientOid\":\"" + clientOid + '\"' +
            ",\n\"side\":\"" + side + '\"' +
            ",\n\"symbol\":\"" + symbol + '\"' +
            ",\n\"type\":\"" + type + '\"' +
            ",\n\"size\":\"" + size + '\"' +
            "\n}";

这是我基于 kucoin-python 包装器创建的代码片段。 你可以测试一下:

from kucoin.client import Trade, Market


class my_kukoin_handler:
def __init__(self,key,sec,pas):
    self.api_key = key
    self.api_secret = sec
    self.api_passphrase = pas
    self.client = Trade(key=self.api_key, secret=self.api_secret, passphrase=self.api_passphrase, is_sandbox=False, url='')

def update_client(self):
    self.client = Trade(key=self.api_key, secret=self.api_secret, passphrase=self.api_passphrase, is_sandbox=False,url='')

def buy(self,coin_no_usdt,qty):
    try:
        order_id = self.client.create_market_order(str(coin_no_usdt).upper().replace('USDT','').strip()+'-USDT', 'buy', size=str(int(qty)))
    except Exception as e :
        print('Kukoin error while buyin has occured',str(e))