WebSocket connection upgrade failed (403 - Forbidden)

vijoeyz

I am facing this problem after upgrading to kiteconnect 3.9.5:
WebSocket connection upgrade failed (403 - Forbidden)
From the previous forum discussions (here and here), I understand that problem occurs if incorrect access_token is used. However, this has never been a problem until I upgraded to 3.9.5, and the same program ran without any problems ever in the previous version, 3.8.1!

Is there a backend problem with my account/keys?




2021-11-29 11:54:57+0530 [-] Log opened.
2021-11-29 11:54:57+0530 [-]
2021-11-29 11:54:57+0530 [-] failing WebSocket opening handshake ('WebSocket connecti
on upgrade failed (403 - Forbidden)')
2021-11-29 11:54:57+0530 [-] dropping connection to peer tcp4:13.235.93.249:443 with
abort=True: WebSocket connection upgrade failed (403 - Forbidden)
2021-11-29 11:54:57+0530 [-] will retry in
2 seconds
2021-11-29 11:54:57+0530 [-] Stopping factory
2021-11-29 11:55:00+0530 [-] Starting factory
2021-11-29 11:55:00+0530 [-] failing WebSocket opening handshake ('WebSocket connecti
on upgrade failed (403 - Forbidden)')
2021-11-29 11:55:00+0530 [-] dropping connection to peer tcp4:13.235.93.249:443 with
abort=True: WebSocket connection upgrade failed (403 - Forbidden)
2021-11-29 11:55:00+0530 [-] will retry in
5 seconds
2021-11-29 11:55:00+0530 [-] Stopping factory
2021-11-29 11:55:06+0530 [-] Starting factory
2021-11-29 11:55:06+0530 [-] failing WebSocket opening handshake ('WebSocket connecti
on upgrade failed (403 - Forbidden)')
2021-11-29 11:55:06+0530 [-] dropping connection to peer tcp4:13.235.93.249:443 with
abort=True: WebSocket connection upgrade failed (403 - Forbidden)
2021-11-29 11:55:06+0530 [-] will retry in
4 seconds
2021-11-29 11:55:06+0530 [-] Stopping factory
2021-11-29 11:55:11+0530 [-] Starting factory
2021-11-29 11:55:11+0530 [-] failing WebSocket opening handshake ('WebSocket connecti
on upgrade failed (403 - Forbidden)')
2021-11-29 11:55:11+0530 [-] dropping connection to peer tcp4:13.235.93.249:443 with
abort=True: WebSocket connection upgrade failed (403 - Forbidden)
2021-11-29 11:55:11+0530 [-] will retry in
5 seconds
2021-11-29 11:55:11+0530 [-] Stopping factory
2021-11-29 11:55:17+0530 [-] Starting factory
2021-11-29 11:55:17+0530 [-] failing WebSocket opening handshake ('WebSocket connecti
on upgrade failed (403 - Forbidden)')
2021-11-29 11:55:17+0530 [-] dropping connection to peer tcp4:13.235.93.249:443 with
abort=True: WebSocket connection upgrade failed (403 - Forbidden)
2021-11-29 11:55:17+0530 [-] will retry in
4 seconds
2021-11-29 11:55:17+0530 [-] Stopping factory
Reconnecting... (attempt: 4 of 500)ecting... (attempt: 1 of 500)
  • vijoeyz
    @sujith @rakeshr
    Quick help on this is appreciated since I have high stakes. Thank you.
  • vijoeyz
    And the code has always been this:

    def init_ticker(api_key, access_token):
    try:

    kd.kws = KiteTicker(api_key, access_token, reconnect_max_delay=5, reconnect_max_tries=300, debug = True)

    kd.kws.on_ticks = on_ticks
    kd.kws.on_close = on_close
    kd.kws.on_error = on_error
    kd.kws.on_message = on_message
    kd.kws.on_connect = on_connect
    kd.kws.on_reconnect = on_reconnect
    kd.kws.on_noreconnect = on_noreconnect
    kd.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.
    kd.kws.connect(threaded = True)

    # Block main thread
    #logging.info("This is main TICKER thread.")

    except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    log.console('%s: init_ticker, %d: %s' % (kd.current_time, exc_tb.tb_lineno, str(e)))


    def get_kite_client():
    global kite_client

    kite_client = KiteConnect(api_key = kite_api_key, debug = True)

    if "access_token" in session:
    kite_client.set_access_token(session["access_token"])

    return kite_client



    @app.route("/login")
    def login():
    global kite_client

    try:
    request_token = request.args.get("request_token")

    kite_client = get_kite_client()
    data = kite_client.generate_session(request_token, api_secret = kite_api_secret)
    kite_client.set_access_token(data['access_token'])

    session['access_token'] = data['access_token']

    init_ticker(kite_api_key, data['access_token'])


    ....

  • rakeshr
    @vijoeyz
    v3.9.5 has breaking changes. You need to upgrade to a stable version v4.0.0. The same was announced here.
    You can upgrade using pip install --upgrade kiteconnect
  • vijoeyz
    I will do that. Meanwhile, I got to know that the ticker field name changes, and have already incorporated. Thanks.
  • vijoeyz
    @rakeshr



    I just upgraded to 4.0.0, but this 403 problem still persists.
    2021-11-29 13:06:13+0530 [-] OPT trades are disabled
    2021-11-29 13:06:13+0530 [-]
    2021-11-29 13:06:13+0530 [-]
    2021-11-29 13:06:13+0530 [-] failing WebSocket opening handshake ('WebSocket connecti
    on upgrade failed (403 - Forbidden)')
    2021-11-29 13:06:13+0530 [-] dropping connection to peer tcp4:13.127.118.127:443 with
    abort=True: WebSocket connection upgrade failed (403 - Forbidden)
    2021-11-29 13:06:13+0530 [-] will retry in
    2 seconds
  • vijoeyz
    @rakeshr

    Please let me know if you need more data. You may appreciate my urgency. I have also tried connect(threaded = True, disable_ssl_verification = True) without success.

    Thank you...
  • rakeshr
    We haven't done any changes from our side. Also, I just tried a similar websocket code flow at my end as you stated above, and it's working perfectly fine.
    Are you able to make other API calls using same access_token? Like order book, portfolio fetch, etc?
  • vijoeyz
    Actually now that mentioned, yes, I can see my positions() and fetch order book!
  • vijoeyz
    @rakeshr I understand the logic of you haven't made any changes. It applies to me as well: I haven't made any change at all. This has been smooth for >2 years!

    Why connection requested is being rejected now, after the upgrade? Give me a clue and I can try figuring it out.
  • vijoeyz
    I can IM you if you need private data to debug.
  • rakeshr
    I don't see any issue in your WebSocket code, except for the access token. Can you re-generate the access token and check again?
    Also, print access_token while making API requests and subscribing to Websocket and compare, if both are the same.
  • rakeshr
    If still facing the issue, you need to print the socket_url and DM me the url. Something like:
    # Callback for successful connection.
    def on_connect(ws, response):
    .....
    print(ws.socket_url)
  • vijoeyz
    vijoeyz edited November 2021
    Can you re-generate the access token and check again
    Does it mean generate using renew_access_token() ?

    This is what generate_session() returned:
    XXXX

  • SRIJAN
    No,Rakesh sir means to log in again like you do every day and generate a new access token.
  • rakeshr
    Please don't post account-related information here. You can DM me such details.
    Does it mean generate using renew_access_token() ?
    No, i mean to re-run login flow and generate access token again. For your flask app, you can visit /login.
    @app.route("/login")
    def login():
    .....
  • vijoeyz
    @rakeshr , Thanks for deleting my private data.

    I may have logged in about 20 times today, but that doesn't help.

    @rakeshr , I have just IM'ed an order executed by the app. All APIs work, it is just that the WS connection fails every time, and therefore, ticks don't arrived.
  • rakeshr
    You were sending an extra quote in your API key for the WebSocket connection. You were sending : ..?api_key='your_api_key'
    correct would be : ..?api_key=your_api_key
    Hence it kept throwing 403.
This discussion has been closed.