How to Subscribe to a Huobi private order notifications?

How to make it right. I want to receive order notificiation?

import websocket
import json
from urllib.parse import urljoin, urlencode
import gzip
import hmac
import hashlib

from datetime import datetime


api_key = 'xxx'
secret_key = 'yyy'

huobi_futures="btc-usdt"  # Adjust the contract code accordingly

endpoint="wss://api.hbdm.com/linear-swap-notification"

def generate_signature(api_key, secret_key, timestamp):
    message = f"GET\n/linear-swap-api/v1/swap_cross_notify\naccessKeyId={api_key}&signatureMethod=HmacSHA256&signatureVersion=2&timestamp={timestamp}"
    signature = hmac.new(secret_key.encode(), message.encode(), hashlib.sha256).hexdigest()
    return signature

def subscribe_to_usdt_futures_market(ws):
    symbol = huobi_futures

    subscribe_data = {
        "op": "sub",
        "topic": f"orders_cross.{symbol}"
    }

    ws.send(json.dumps(subscribe_data))

def on_message(ws, message):
    print("Received message:")
    try:
        json_message = json.loads(gzip.decompress(message).decode("utf-8"))
        print(json_message)
    except json.JSONDecodeError as e:
        print(f"Error decoding message: {e}")

def on_error(ws, error):
    print(f"Error: {error}")

def on_close(ws, close_status_code, close_msg):
    print("Closed")

def on_open(ws):

        timestamp = str(datetime.utcnow().isoformat())[0:19]
        signature = generate_signature(api_key, secret_key, timestamp)

        auth_data = {
            "op": "auth",
            "type": "api",
            "AccessKeyId": api_key,
            "SignatureMethod": "HmacSHA256",
            "SignatureVersion": 2,
            "Timestamp": timestamp,
            "Signature": signature
        }

        ws.send(json.dumps(auth_data))





        subscribe_to_usdt_futures_market(ws)


if __name__ == "__main__":
    ws = websocket.WebSocketApp(endpoint, on_message=on_message, on_error=on_error, on_close=on_close)
    ws.on_open = on_open
    ws.run_forever()

I am always getting error

Received message: {‘op’: ‘auth’, ‘type’: ‘api’, ‘ts’: 1701522178042, ‘err-code’: 2003, ‘err-msg’: ‘Verification failure [校验失败]’} Received message: {‘op’: ‘sub’, ‘topic’: ‘orders_cross.btc-usdt’, ‘err-code’: 2002, ‘err-msg’: ‘Authentication required.’, ‘ts’: 1701522178046}

.yfyfybiukhoukghouk,mv

Leave a Comment