Getting exception while downloading "Historical Data"

avinashzen123
Even though i have subscribed to historical data getting exception "kiteconnect.exceptions.TokenException: Incorrect `api_key` or `access_token`." while downloading historical data
  • sujith
    Can you enabled debug logs of pykiteconnect and share the complete stacktrace of that? Please do share on private message. It looks like you are not passing a valid api_key or access_token.
  • avinashzen123
    @sujith below is complete stack trace

    Traceback (most recent call last):
    File "D:\PythonProjects\AngelBrokerImpl\algo_trading_app\broker\main.py", line 24, in main
    symbol_data = historial_data.download_historical_data(symbol)
    File "D:\PythonProjects\AngelBrokerImpl\algo_trading_app\broker\angel\HistoricalData.py", line 12, in download_historical_data
    return self.kite.historical_data(symbol.instrument_token, date_after_days(TODAY_DATE_TIME, days=days),
    File "D:\PythonProjects\AngelBrokerImpl\.venv\lib\site-packages\kiteconnect\connect.py", line 632, in historical_data
    data = self._get("market.historical",
    File "D:\PythonProjects\AngelBrokerImpl\.venv\lib\site-packages\kiteconnect\connect.py", line 861, in _get
    return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
    File "D:\PythonProjects\AngelBrokerImpl\.venv\lib\site-packages\kiteconnect\connect.py", line 937, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.TokenException: Incorrect `api_key` or `access_token`.

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "D:\PythonProjects\AngelBrokerImpl\algo_trading_app\broker\main.py", line 34, in
    main()
    File "D:\PythonProjects\AngelBrokerImpl\algo_trading_app\broker\main.py", line 27, in main
    print(e.with_traceback())
    TypeError: BaseException.with_traceback() takes exactly one argument (0 given)
  • sujith
    sujith edited January 31
    You need to set debug flag true as mentioned here while initializing the kite object.

    Make sure to private message the stacktrace as it will contain app and client specific tokens.
  • avinashzen123
    avinashzen123 edited January 31
    Below is complete code with logs, kindly let me know if what change should i do, Complete line of one hidden line is

    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/112391431/minute?from=2025-01-29+14%3A55%3A44&to=2025-01-31+14%3A55%3A44&interval=minute&continuous=0&oi=1 HTTP/1.1" 403 None



  • avinashzen123
    avinashzen123 edited January 31
    @sujith Same HTTP 403 Error code i am receiving while connecting webcoket

    D:\PythonProjects\AngelBrokerImpl\.venv\Scripts\python.exe D:\PythonProjects\AngelBrokerImpl\algo_trading_app\broker\angel\DataStream.py
    ERROR:kiteconnect.ticker:Connection error: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
    ERROR:kiteconnect.ticker:Connection closed: 1006 - connection was closed uncleanly (WebSocket connection upgrade failed (403 - Forbidden))
  • avinashzen123
    Log with "debug=True"


  • avinashzen123
    @sujith Even i tried postman for fetching data getting same HTTP 403 code

  • sujith
    We need to see the headers to conclude anything. The error message says you are sending an invalid api_key or access_token.
Sign In or Register to comment.