api_secret etc. not left blank in run code. only to hide it here. get output : {'code': '400005', 'msg': 'Invalid KC-API-SIGN'} it works for other POST orders, eg. buy/sell.
def borrow_margin(currency, amount):
api_key = ****************
api_secret = ****************
api_passphrase = ****************
url = 'https://api.kucoin.com/api/v1/margin/borrow'
order = {'currency':currency,'type':'FOK','size':amount}
order_json = json.dumps(order)
now = int(time.time() * 1000)
str_to_sign = str(now) + 'POST' + '/api/v1/margin/borrow' + order_json
signature = base64.b64encode(hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest())
passphrase = base64.b64encode(hmac.new(api_secret.encode('utf-8'), api_passphrase.encode('utf-8'), hashlib.sha256).digest())
headers = {"KC-API-SIGN":signature,
"KC-API-TIMESTAMP":str(now),
"KC-API-KEY":api_key,
"KC-API-PASSPHRASE":passphrase,
"KC-API-KEY-VERSION":"2",
"Content-Type":"application/json"
}
req = requests.request('post', url, headers=headers).json()
print(req)
borrow_margin('USDT', 50.0)
output : {'code': '400005', 'msg': 'Invalid KC-API-SIGN'}
should be : req = requests.request('POST', url=url, headers=headers, data=order_json).json()