Create order with Python script in kucoin return kc invalid sign

62 Views Asked by At

This is my code:

import base64
import hashlib
import hmac
import json
import time

import requests

api_key = '.....'
api_secret = '.....'
api_passphrase = '....'

url = 'https://api.kucoin.com/api/v1/orders'
# url = 'https://api.kucoin.com/api/v1/accounts?type=trade'

now = int(time.time() * 1000)
params = {"clientOid": "AAAjhjhjhjk",
          "side": "BUY",
          "symbol": "BTC-USDT",
          "type": "limit",
          "size": "0.001",
          "price": "41220.9",
          "postOnly": "true"}

json_params = json.dumps(params)

str_to_sign = str(now) + 'POST' + '/api/v1/orders' + json_params
print(str_to_sign)
# str_to_sign = str(now + 100) + 'GET' + '/api/v1/accounts?type=trade'

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",

}

response = requests.request('post', url, headers=headers, params=params)
print(response.status_code)
print(response.json())

I think everything is ok but I got kc invalid sign! When I call get account every thing works well (URL which I comment), but I got error with place order!

i dont want use kucoin clinet, i want to find problem with this code, can any body help me?

1

There are 1 best solutions below

0
On

KuCoin expects the signature to be a string, but you are currently encoding it as bytes using base64.b64encode. You should convert the bytes to a string before setting it in the headers.

import base64
import hashlib
import hmac
import json
import time

import requests

api_key = '.....'
api_secret = '.....'
api_passphrase = '....'

url = 'https://api.kucoin.com/api/v1/orders'

now = int(time.time() * 1000)
params = {"clientOid": "AAAjhjhjhjk",
          "side": "BUY",
          "symbol": "BTC-USDT",
          "type": "limit",
          "size": "0.001",
          "price": "41220.9",
          "postOnly": "true"}

json_params = json.dumps(params)

str_to_sign = str(now) + 'POST' + '/api/v1/orders' + json_params

signature_bytes = hmac.new(api_secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha256).digest()
signature = base64.b64encode(signature_bytes).decode('utf-8')  

passphrase = base64.b64encode(api_passphrase.encode('utf-8')).decode('utf-8')

headers = {
    "KC-API-SIGN": signature,
    "KC-API-TIMESTAMP": str(now),
    "KC-API-KEY": api_key,
    "KC-API-PASSPHRASE": passphrase,
    "KC-API-KEY-VERSION": "2",
}

response = requests.request('post', url, headers=headers, json=params)  
print(response.status_code)
print(response.json())
  • Decode the signature and passphrase to UTF-8 before including them in the headers.
  • Use json=params instead of params=params in the request to ensure the data is sent as JSON.

This should help resolve the "invalid sign" issue. If the problem persists, double-check that your API key, secret, and passphrase are correct, and ensure that your system time is accurate.