Invalid auth token. WebSocket ( even access_token is correct )

Sir0519
import logging
from kiteconnect import KiteTicker

logging.basicConfig(level=logging.DEBUG)

# Initialise
kws = KiteTicker("your_api_key", "your_access_token")

def on_ticks(ws, ticks): # noqa
# Callback to receive ticks.
logging.info("Ticks: {}".format(ticks))

def on_connect(ws, response): # noqa
# Callback on successful connect.
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe([738561, 5633])

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, [738561])

def on_order_update(ws, data):
logging.debug("Order update : {}".format(data))

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_order_update = on_order_update

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()






ERROR

runfile('D:/VruAlgo/untitled24.py', wdir='D:/VruAlgo')
--- request header ---
DEBUG:websocket:--- request header ---
GET / HTTP/1.1
Upgrade: websocket
Host: ws.kite.trade
Origin: https://ws.kite.trade
Sec-WebSocket-Key: HJOgCYcxC+VEceVPWr97HQ==
Sec-WebSocket-Version: 13
Connection: Upgrade


DEBUG:websocket:GET / HTTP/1.1
Upgrade: websocket
Host: ws.kite.trade
Origin: https://ws.kite.trade
Sec-WebSocket-Key: HJOgCYcxC+VEceVPWr97HQ==
Sec-WebSocket-Version: 13
Connection: Upgrade


-----------------------
DEBUG:websocket:-----------------------
--- response header ---
DEBUG:websocket:--- response header ---
HTTP/1.1 400 Bad Request
DEBUG:websocket:HTTP/1.1 400 Bad Request
Date: Thu, 09 May 2024 10:55:04 GMT
DEBUG:websocket:Date: Thu, 09 May 2024 10:55:04 GMT
Content-Type: application/json; charset=utf-8
DEBUG:websocket:Content-Type: application/json; charset=utf-8
Content-Length: 50
DEBUG:websocket:Content-Length: 50
Connection: keep-alive
DEBUG:websocket:Connection: keep-alive
-----------------------
DEBUG:websocket:-----------------------
Handshake status 400 Bad Request -+-+- {'date': 'Thu, 09 May 2024 10:55:04 GMT', 'content-type': 'application/json; charset=utf-8', 'content-length': '50', 'connection': 'keep-alive'} -+-+- b'{"status":"error","message":"Invalid auth token."}' - goodbye
ERROR:websocket:Handshake status 400 Bad Request -+-+- {'date': 'Thu, 09 May 2024 10:55:04 GMT', 'content-type': 'application/json; charset=utf-8', 'content-length': '50', 'connection': 'keep-alive'} -+-+- b'{"status":"error","message":"Invalid auth token."}' - goodbye
error from callback : 'WebSocketApp' object has no attribute 'stop'
ERROR:websocket:error from callback : 'WebSocketApp' object has no attribute 'stop'
WebSocket error: Handshake status 400 Bad Request -+-+- {'date': 'Thu, 09 May 2024 10:55:04 GMT', 'content-type': 'application/json; charset=utf-8', 'content-length': '50', 'connection': 'keep-alive'} -+-+- b'{"status":"error","message":"Invalid auth token."}'
WebSocket error: 'WebSocketApp' object has no attribute 'stop'
Tagged:
  • rakeshr
    WebSocket error: 'WebSocketApp' object has no attribute 'stop'
    You are not setting stop callback in the above-mentioned websocket code. Source of this error seems to be different.
  • Sir0519
    import logging
    from kiteconnect import KiteTicker

    logging.basicConfig(level=logging.DEBUG)

    # Construct WebSocket URL with API key and access token
    websocket_url = "wss://ws.kite.trade?api_key=YOUR_API_KEY&access_token=YOUR_ACCESS_TOKEN"

    # Initialise KiteTicker with the WebSocket URL
    kws = KiteTicker("YOUR_API_KEY", "YOUR_ACCESS_TOKEN", websocket=websocket_url)

    # Define callback functions
    def on_ticks(ws, ticks):
    # Callback to receive ticks.
    logging.info("Ticks: {}".format(ticks))

    def on_connect(ws, response):
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe([738561, 5633])

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, [738561])

    def on_order_update(ws, data):
    logging.debug("Order update : {}".format(data))

    def on_close(ws, close_status_code, close_msg):
    # Handle WebSocket close event
    print("WebSocket connection closed with status code:", close_status_code)
    print("Close message:", close_msg)

    # Assign the callbacks
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_order_update = on_order_update
    kws.on_close = on_close

    # Connect to the WebSocket server
    kws.connect()






    STILL SHOWING

    runfile('D:/VruAlgo/untitled2.py', wdir='D:/VruAlgo')
    ERROR:websocket:Handshake status 400 Bad Request -+-+- {'date': 'Sun, 12 May 2024 11:59:20 GMT', 'content-type': 'application/json; charset=utf-8', 'content-length': '50', 'connection': 'keep-alive'} -+-+- b'{"status":"error","message":"Invalid auth token."}' - goodbye
    WebSocket error: Handshake status 400 Bad Request -+-+- {'date': 'Sun, 12 May 2024 11:59:20 GMT', 'content-type': 'application/json; charset=utf-8', 'content-length': '50', 'connection': 'keep-alive'} -+-+- b'{"status":"error","message":"Invalid auth token."}'
    WebSocket connection closed with status code: None
    Close message: None
Sign In or Register to comment.