Kucoin API“无效的KC-API-SIGN”

Kucoin API 'Invalid KC-API-SIGN'

提问人:Jaysin Koregaokar 提问时间:1/22/2023 最后编辑:Jaysin Koregaokar 更新时间:7/11/2023 访问量:823

问:

api_secret等,在运行代码中不留空。只是为了把它藏在这里。 get output : {'code': '400005', 'msg': '无效的KC-API-SIGN'} 它适用于其他 POST 订单,例如。买入/卖出。

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)

输出: {'code': '400005', 'msg': '无效的KC-API-SIGN'}

python kucoin 借用

评论

0赞 Jaysin Koregaokar 1/22/2023
为什么我会收到此错误: {'code': '400005', 'msg': '无效的KC-API-SIGN'}

答:

0赞 Jaysin Koregaokar 1/23/2023 #1

应为: req = requests.request('POST', url=url, headers=headers, data=order_json).json()

评论

0赞 Community 1/24/2023
正如目前所写的那样,你的答案尚不清楚。请编辑以添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。您可以在帮助中心找到有关如何写出好答案的更多信息。
0赞 Anonymouse 7/11/2023 #2

这对我来说也是一个令人头疼的问题。这是我当前(工作)期货API的代码:如何获取账户信息,如何开仓,如何平仓。

关于KC_place_order(curr、side、amount)的澄清: *参数 'curr' 是指您正在使用的交易对:例如,字符串值如 'FTM-USDT'、'ETH-USDT' 等。 *'side' 是多头或空头头寸的字符串值:例如 'buy' 或 'sell' *“金额”是您喜欢交易的货币数量的浮动值。

我知道,代码并不完美,但它正在工作。顺便说一句,现货 API 的代码会略有不同:问我是否有人也希望我发布它。

祝你好运!=)

    import time
    import base64
    import hmac
    import hashlib
    import requests
    import json
    import uuid
    import datetime
    from dateutil import parser
    import pandas as pd
    import numpy as np
    import os

def get_equity_account(): 

    client0id = uuid.uuid4().hex
    client0id = str(client0id)
    api_key = "your api_key"
    api_secret = "your api_secret"
    api_passphrase = "your api_passphrase"

    #Get account equity in USDT
    url = 'https://api-futures.kucoin.com/api/v1/account-overview?currency=USDT'
    now = int(time.time() * 1000)
    data = {"clientOid": client0id,'currency':'FTM'}
    data_json = json.dumps(data)
    str_to_sign = str(now) + 'GET' + '/api/v1/account-overview?currency=USDT' + data_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" # specifying content type or using json=data in request
    }
    response = requests.request('GET', url, headers=headers, data=data_json)
    # print(response.status_code)
    # pprint(response.json())
    # print(response.json()['data']['accountEquity'])
    equity = float(response.json()['data']['accountEquity'])
    return equity

    # server message example: 
    #     {'code': '200000',
    #  'data': {'accountEquity': 19834.517194515,
    #           'availableBalance': 19834.517194515,
    #           'currency': 'USDT',
    #           'frozenFunds': 0,
    #           'marginBalance': 19834.517194515,
    #           'orderMargin': 0.0,
    #           'positionMargin': 0,
    #           'unrealisedPNL': 0}}


def KC_place_order(curr, side, amount):

    leverage = '< float with whatever leverage you prefer >'

    # curr_URL = curr.replace('-', '') + 'M' - only for futures trading. Original curr string for spot
    curr_replace = curr.replace('-','_')
    curr_URL = curr.replace('-', '') + 'M'
    curr_equity_symbol = curr.replace('-USDT', '')
    client0id = uuid.uuid4().hex
    client0id = str(client0id)
    api_key = "your api_key"
    api_secret = "your api_secret"
    api_passphrase = "your api_passphrase"
    url = 'https://api-futures.kucoin.com/api/v1/orders'
    now = int(time.time() * 1000)
    data = {"clientOid": client0id, "symbol": curr_URL, "side": side, "type": "market", 'leverage': leverage, "size": amount} #how to set stop loss? Check dev docs!
    data_json = json.dumps(data)
    str_to_sign = str(now) + 'POST' + '/api/v1/orders' + data_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" # specifying content type or using json=data in request
        }
    response = requests.request('POST', url, headers=headers, data=data_json)
    print(response.status_code)
    print(response.json())

    #"closeOrder": True,
    #"side": "sell"
    #'leverage': 5
    #data = {"clientOid": client0id,"symbol": "FTMUSDTM", "side": "buy", "type": "market",'leverage': '5', "size": 10}
    #KC_place_order(curr, side, amount, equity_deal)
    return response

def KC_close_position(curr):

    #get equity of a certain currency
    #sell the enirety of that equity
    curr_URL = curr.replace('-', '') + 'M'
    curr_replace = curr.replace('-','_')
    curr_equity_symbol = curr.replace('-USDT', '')

    client0id = uuid.uuid4().hex
    client0id = str(client0id)
    api_key = "your api_key"
    api_secret = "your api_secret"
    api_passphrase = "your api_passphrase"
    url = 'https://api-futures.kucoin.com/api/v1/orders'
    now = int(time.time() * 1000)
    data = {"clientOid": client0id,"symbol": curr_URL, "type": "market", "closeOrder": True,} 
    data_json = json.dumps(data)
    str_to_sign = str(now) + 'POST' + '/api/v1/orders' + data_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" # specifying content type or using json=data in request
    }
    response = requests.request('POST', url, headers=headers, data=data_json)
    print(response.status_code)
    print(response.json())


    return response