简体   繁体   中英

How to pass token to get connected with Websocket API using python Websocket-client lib

Intro

I am trying to access the order book data from Indodax exchange using Websocket API . Doc can be found here. . I am using python 3.9 with websocket-client library.

Issue

It is giving me bad request after configuring everything as per the doc . I have enabled token during handshake following this example given in this this answer .

--- request header ---
GET /ws/ HTTP/1.1
Upgrade: websocket
Host: ws.indodax.com
Origin: http://ws.indodax.com
Sec-WebSocket-Key: X+jRgd8pM0XefcT1/xwHNQ==
Sec-WebSocket-Version: 13
Connection: Upgrade
extension-token:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaW5mbyI6eyJuYW1lIjoiUHVibGljIn19.VJAHTrrfwxceSITpuPBl75LVM5bgojKGiUTOwCZxw-k


-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Server: nginx
Date: Wed, 20 Oct 2021 10:41:13 GMT
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: rI37u4yeNItEuZP5MeqZ254XGuo=
Via: 1.1 google
Alt-Svc: clear
-----------------------
++Sent raw: b'\x81\x8eb\xb2\x18\xc9@\xd0l\xaa\x0b\xd6j\xe7\x16\xc0y\xad\x07\x90'
++Sent decoded: fin=1 opcode=1 data=b'"btcidr.trade"'
thread terminating...
++Rcv raw: b'\x88,\x0b\xbb{"reason":"bad request","reconnect":false}'
++Rcv decoded: fin=1 opcode=8 data=b'\x0b\xbb{"reason":"bad request","reconnect":false}'
++Sent raw: b'\x88\x82\xc3Q K\xc0\xb9'
++Sent decoded: fin=1 opcode=8 data=b'\x03\xe8'
### closed ###

Code

import websocket
import json
import _thread


def on_message(ws, message):
    print(message)

def on_error(ws, error):
    print(error)

def on_close(ws, close_status_code, close_msg):
    print("### closed ###")

def on_open(ws):
    subscribeEvent(ws, "btcidr"+".trade")
    print("thread terminating...")
    _thread.start_new_thread(send_heartbeat, ())x`

def send_heartbeat(*args):
    pass

def subscribeEvent(ws, event, auth_key=''):
    try:
        ws.send(json.dumps(event))
    except Exception as e:
        print(e)

if __name__ == "__main__":
    websocket.enableTrace(True)
    token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIiwiaW5mbyI6eyJuYW1lIjoiUHVibGljIn19.VJAHTrrfwxceSITpuPBl75LVM5bgojKGiUTOwCZxw-k'
    protocol_str = "extension-token:" + token
    while True:
        ws = websocket.WebSocketApp("wss://ws.indodax.com/ws/",
                                  on_open=on_open,
                                  on_message=on_message,
                                  on_error=on_error,
                                  on_close=on_close,
                                  header = [protocol_str])
        ws.run_forever()

Additional Info

I believe the issue lies in the token. I mean some formatting issue about how should I be passing the token to the server. Let me know if you need any additional info.

Add your token as optional parameter in the URL. WS_HOST in this snippet, could be your wss://ws.indodax.com/ws/

URL = '{}?token={}'.format(WS_HOST, TOKEN)

ws = websocket.WebSocketApp(URL,
                                  on_open=on_open,
                                  on_message=on_message,
                                  on_error=on_error,
                                  on_close=on_close,
                                  header = [protocol_str])
        ws.run_forever()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM