[Python] Error Message 10007 BitUnix Signature Error when trying to change margin

Discussão em 'Python' iniciado por Stack, Maio 14, 2025.

  1. Stack

    Stack Membro Participativo

    im just Completely lost with my code, i have now tried for multiple hours and used every ai tool imaginable but i just can get it rigth. Using the BitUnix api via responses i always get a sigature error. This is my code:

    def auth_headers(self, query_params: dict = None, body: dict = None) -> dict: """ Generate signed authentication headers for Bitunix API using strict formatting: - query_params: dict used in signature (for GET requests or query parameters) - body: dict used in signature (for POST requests) """ import json import hashlib

    def sha256_hex(input_string):
    return hashlib.sha256(input_string.encode('utf-8')).hexdigest()

    # Generate nonce (32-bit random string) and timestamp
    nonce = str(int(time.time() * 1000))[-6:] # Last 6 digits like "123456"
    timestamp = str(int(time.time() * 1000)) # Milliseconds timestamp

    # 1. Sort query params in ascending ASCII order by key
    query_str = ""
    if query_params:
    # Convert all values to strings and sort by key
    sorted_params = sorted(query_params.items())
    query_str = ''.join(f"{k}{str(v)}" for k, v in sorted_params)

    # 2. Convert body to string with no spaces (if present)
    body_str = "uid1234"
    if body:
    body_str = json.dumps(body, separators=(',', ':'), ensure_ascii=True)
    print(body_str)

    # 3. First hash: SHA256(nonce + timestamp + apiKey + queryParams + body)
    pre_digest = nonce + timestamp + self.api_key + query_str + body_str
    digest = sha256_hex(pre_digest)

    # 4. Final sign: SHA256(digest + secretKey)
    sign = sha256_hex(digest + self.secret_key)

    # Debug info
    print("Nonce:", nonce)
    print("Timestamp:", timestamp)
    print("Query String:", query_str)
    print("Body String:", body_str)
    print("Pre-digest:", pre_digest)
    print("Digest:", digest)
    print("Sign:", sign)

    return {
    'api-key': self.api_key,
    'sign': sign,
    'nonce': nonce,
    'timestamp': timestamp,
    'Content-Type': 'application/json'
    }


    def change_marginmode(self):
    """
    Change margin mode following the exact signature pattern from documentation.
    """
    url = 'https://fapi.bitunix.com/api/v1/futures/account/change_margin_mode'

    # Query parameters must be included in signature
    query_params = {
    "symbol": "BTCUSDT",
    "marginCoin": "USDT",
    "marginMode": "ISOLATION"
    }


    when i now call the method i get this error: {'code': 10007, 'data': None, 'msg': 'Signature Error'}

    does anyone know what i did wrong?

    Continue reading...

Compartilhe esta Página