kiteconnect.exceptions.TokenException: Incorrect `api_key` or `access_token`.

sanoop
I am getting an error while access the historical price, however instrument list API returns the successful outcome. Below is my output where instrument tokens are being fetched and Historical Data API gives the error -

/opt/anaconda3/bin/python /Users/sanoop/PycharmProjects/Anagha/com/com/rapid/execution.py
[1793, 3329, 5633, 6401, 40193, 54273, 56321, 60417, 70401, 79873, 81153, 82945, 85761, 87553, 98049, 108033, 112129, 119553, 130305, 134657, 140033, 160769, 163073, 173057, 175361, 177665, 194561, 197633, 225537, 232961, 261889, 303361, 315393, 325121, 341249, 345089, 348929, 356865, 359937, 364545, 387073, 408065, 415745, 424961, 486657, 492033, 502785, 511233, 519425, 519937, 548353, 548865, 558337, 579329, 589569, 593665, 617473, 633601, 681985, 738561, 756481, 758529, 779521, 784129, 794369, 806401, 824321, 837889, 857857, 860929, 871681, 873217, 877057, 878593, 884737, 895745, 897537, 900609, 912129, 951809, 952577, 969473, 1041153, 1076225, 1086465, 1102337, 1152769, 1195009, 1199105, 1207553, 1214721, 1215745, 1270529, 1304833, 1346049, 1471489, 1510401, 1552897, 1629185, 1675521, 1703937, 1716481, 1723649, 1790465, 1850625, 1895937, 2010113, 2029825, 2067201, 2127617, 2170625, 2426881, 2445313, 2455041, 2457345, 2478849, 2513665, 2585345, 2615553, 2622209, 2672641, 2674433, 2714625, 2730497, 2748929, 2752769, 2763265, 2800641, 2815745, 2863105, 2865921, 2876417, 2883073, 2889473, 2905857, 2911489, 2919169, 2939649, 2952193, 2953217, 2955009, 2962177, 2977281, 2995969, 3001089, 3023105, 3039233, 3050241, 3095553, 3343617, 3400961, 3407361, 3412993, 3419905, 3463169, 3465729, 3484417, 3520257, 3529217, 3623425, 3660545, 3663105, 3677697, 3689729, 3691009, 3725313, 3735553, 3771393, 3834113, 3861249, 3876097, 3920129, 3924993, 3930881, 3937281, 3945985, 4267265, 4268801, 4359425, 4376065, 4454401, 4464129, 4488705, 4561409, 4574465, 4576001, 4592385, 4598529, 4600577, 4632577, 4644609, 4701441, 4708097, 4774913, 4834049, 4840449, 4843777, 4879617, 4923905, 5013761, 5097729, 5181953, 5186817, 5195009, 5197313, 5215745, 5331201, 5420545, 5436929, 5533185, 5552641, 5573121, 5582849, 5619457, 5728513, 6054401, 6191105, 6386689, 6491649, 6599681, 7458561, 7707649, 7982337, 8042241]
0%| | 0/224 [00:00<?, ?it/s]AARTIIND
Traceback (most recent call last):
File "/Users/sanoop/PycharmProjects/Anagha/com/com/rapid/execution.py", line 87, in <module>
joblib.Parallel(n_jobs=1)(
File "/opt/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 1918, in __call__
return output if self.return_generator else list(output)
File "/opt/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 1847, in _get_sequential_output
res = func(*args, **kwargs)
File "/Users/sanoop/PycharmProjects/Anagha/com/com/rapid/execution.py", line 30, in execute_for_equities
kite_connect.historical_data(instrument_token=instrument_token, from_date=CONST.from_date,
File "/opt/anaconda3/lib/python3.9/site-packages/kiteconnect/connect.py", line 632, in historical_data
data = self._get("market.historical",
File "/opt/anaconda3/lib/python3.9/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 "/opt/anaconda3/lib/python3.9/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`.
0%| | 0/224 [00:00<?, ?it/s]

Process finished with exit code 1

Tagged:
  • Sravanthi_bh
    Your access token might have expired. You'll need to generate a new access token. Check out the documentation for the login flow.
  • sanoop
    Access Token generated just before I hit the execution; and Instrument List I am able to get
  • sujith
    The instruments master doesn't have authentication for legacy reasons. You may check out this thread and see if you are not doing this.
  • sanoop
    No, I am not doing that.. My Access Token is same since morning no matter how many time I generated with the Request Token.

    process:
    1. with api_key, api_secret get the request token
    2. with request token get the access token

    def get_login(api_key, api_secret):
    global kws, kite
    kite = KiteConnect(api_key=api_key)

    print("[*] Generate access token: ", kite.login_url())
    request_tkn = input("[*] Enter your request token here: ")
    data = kite.generate_session(request_tkn, api_secret=api_secret)
    kite.set_access_token(data["access_token"])
    kws = KiteTicker(api_key, data["access_token"])
    print('Your Access Token is: ', data["access_token"])


    get_login(api_key, api_secret)

    3. use access token and api key to get the kite connection

    zerodha = Zerodha(api_key, access_token)
    kite_connect = zerodha.getKiteConnect()

    4. call api on kite connection object

    data_df = pd.DataFrame(
    kite_connect.historical_data(instrument_token=instrument_token, from_date=CONST.from_date,
    to_date=CONST.to_date,
    interval=CONST.data_interval))



    Is there anything wrong in the process ?
  • Nivas
    Can you enable debug logs of Kite Connect library and share the complete stack trace in a private message?
  • sanoop
    I am aware of logging.basicConfig(level=logging.DEBUG) this code to enable debug.
    with this I get 403


    0%| | 0/1 [00:00<?, ?it/s]DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "GET /instruments/historical/60417/day?from=2020-07-04+18%3A34%3A12&to=2025-07-03+18%3A34%3A12&interval=day&continuous=0&oi=0 HTTP/11" 403 None
    Traceback (most recent call last):
    File "/Users/sanoop/PycharmProjects/Anagha/com/com/rapid/execution.py", line 91, in <module>
    joblib.Parallel(n_jobs=1)(
    File "/opt/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 1918, in __call__
    return output if self.return_generator else list(output)
    File "/opt/anaconda3/lib/python3.9/site-packages/joblib/parallel.py", line 1847, in _get_sequential_output
    res = func(*args, **kwargs)
    File "/Users/sanoop/PycharmProjects/Anagha/com/com/rapid/execution.py", line 34, in execute_for_equities
    kite_connect.historical_data(instrument_token=instrument_token, from_date=CONST.from_date,
    File "/opt/anaconda3/lib/python3.9/site-packages/kiteconnect/connect.py", line 632, in historical_data
    data = self._get("market.historical",
    File "/opt/anaconda3/lib/python3.9/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 "/opt/anaconda3/lib/python3.9/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`.
    0%| | 0/1 [00:00<?, ?it/s]

    Process finished with exit code 1





    is there any other way to enable debug logs of Kite Connect library ?
  • Nivas
    You need to set the debug flag to true while creating the Kite Connect object. You can check out the documentation here.
  • sanoop
    sanoop edited July 4


    /opt/anaconda3/bin/python /Users/sanoop/PycharmProjects/Anagha/com/com/rapid/zerodha_connect.py
    [*] Generate access token: https://kite.zerodha.com/connect/login?api_key=*************&v=3
    [*] Enter your request token here: ******FromURL**********
    DEBUG:kiteconnect.connect:Request: POST https://api.kite.trade/session/token {'api_key': '*********', 'request_token': '***********', 'checksum': 'cfc352098968cdf81697056cc3d264626e2f337118425fe951bcc9d4285c8f02'} {'X-Kite-Version': '3', 'User-Agent': 'Kiteconnect-python/5.0.1'}
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/11" 200 None
    DEBUG:kiteconnect.connect:Response: 200 b'{"status":"success","data":{"user_type":"individual/res_no_nn","email":"san****.****@gmail.com","user_name":"Sanoop Kumar Yadav","user_shortname":"Sanoop","broker":"ZERODHA","exchanges":["NSE","BSE","NFO","MF"],"products":["CNC","NRML","MIS","BO","CO"],"order_types":["MARKET","LIMIT","SL","SL-M"],"avatar_url":null,"user_id":"*******","api_key":"lqly78z223hbmgs3","access_token":"********","public_token":"CduL55L2G5cniQV18MdcQIchtlOufzvL","refresh_token":"","enctoken":"****************","login_time":"2025-07-04 13:26:34","meta":{"demat_consent":"physical"}}}'
    Your Access Token is: **************Generated****************

  • sanoop
    oopppss... I have multi modules running in the project and the was a typo on the import. It was importing incorrect path
  • sanoop
    Thank you All
Sign In or Register to comment.