Getting this error: WebSocket connection upgrade failed (403 - Forbidden))

rv4550
I have gone through all the threads related to this but not able to get around this error. I am using the following code.


import logging
from kiteconnect import KiteConnect
import logging
from kiteconnect import KiteTicker


logging.basicConfig(level=logging.DEBUG)

api_key="xxxxxxxxxxxxxxxxxxx"
secret_key="xxxxxxxxxxxxxxxxxxxxx"

kite = KiteConnect(api_key=api_key)
kite.login_url()

request_token='xxxxxxxxxxxxxxxxxxxxxx'

data = kite.generate_session(request_token, api_secret=secret_key)
kite.set_access_token(data["access_token"])
access_token=data['access_token']

logging.basicConfig(level=logging.DEBUG)

def on_ticks(ws, ticks):
# Callback to receive ticks.
logging.debug("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([408065])

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

def on_close(ws, code, reason):
# On connection close stop the main loop
# Reconnection will not happen after executing `ws.stop()`
ws.stop()



kws = KiteTicker(api_key, access_token)
# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close

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

kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect()

Response:
ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
  • tahseen
    Can you give a GitHub link to your code as this code has indentation issue here
  • tahseen
    Before all that please ensure that you access_token is passed to your code. Historically on this forum, this error in most of the cases has been related developers not passing their access token correctly or were not even aware that there was no token passed
  • rv4550
    Pardon me for the indentation issue while pasting.

    # I set the access token first this way after getting the request token.

    data = kite.generate_session(request_token, api_secret=secret_key)
    kite.set_access_token(data["access_token"])
    access_token=data['access_token']

    # Then i passed it to the object as given on github.
    kws = KiteTicker(api_key, access_token)

    But for some reason its giving this error. Dont know what i am missing here.
  • tahseen
    Have you printed the value in request token and access token to see if you are even getting the values correctly in the first place ?
  • rv4550
    Yes i am getting both request token and access token
Sign In or Register to comment.