[英]How to pass token to get connected with Websocket API using python Websocket-client lib
I am trying to access the order book data from Indodax exchange using Websocket API .我正在尝试使用Websocket API从Indodax交换访问订单簿数据。 Doc can be found here.
文档可以在这里找到。 .
. I am using python 3.9 with websocket-client library.
我正在使用带有websocket-client库的 python 3.9。
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 .
我已经按照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 ###
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()
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.在 URL 中添加您的令牌作为可选参数。 WS_HOST in this snippet, could be your wss://ws.indodax.com/ws/
此片段中的 WS_HOST 可能是您的 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.