Python and Kite connect API (TokenException Error)

chat_thendral
Hi,
I am getting this error kiteconnect.exceptions.TokenException: Incorrect `api_key` or `access_token`.

I have followed following steps.
1. 500 subscription bought and Active
2. Static IP updated in my profile
3. New App created and API KEY and API SECRET fetched
4. Using python kite connect kit installed and further using API Key and Secret Token fetched
5. using API KEY, API Secret and Token successfully logged in (where python can get my name and trade ID properly).
6. Verified using below code: printf 'KEY=[%s] len=%d\n' "$KITE_API_KEY" $(printf '%s' "$KITE_API_KEY" | wc -c)
printf 'TOKEN start=[%s] len=%d\n' "$(printf '%s' "$KITE_ACCESS_TOKEN" | cut -c1-6)" $(printf '%s' "$KITE_ACCESS_TOKEN" | wc -c)
7. then I tested this for final ensure
python3 -c "import os; from kiteconnect import KiteConnect; k=KiteConnect(os.getenv('KITE_API_KEY')); k.set_access_token(os.getenv('KITE_ACCESS_TOKEN')); print('OK', k.profile()['user_name'])"

Got the above error. Full output message is below

Traceback (most recent call last):
File "", line 1, in
import os; from kiteconnect import KiteConnect; k=KiteConnect(os.getenv('KITE_API_KEY')); k.set_access_token(os.getenv('KITE_ACCESS_TOKEN')); print('OK', k.profile()['user_name'])
~~~~~~~~~^^
File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages/kiteconnect/connect.py", line 336, in profile
return self._get("user.profile")
~~~~~~~~~^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages/kiteconnect/connect.py", line 872, in _get
return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.14/lib/python3.14/site-packages/kiteconnect/connect.py", line 948, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.TokenException: Incorrect `api_key` or `access_token`.
  • chat_thendral
    Some one please advise solution
  • salim_chisty
    The error message indicates that your access token might have expired. To resolve this, you need to fetch a new access token. Please note that the exchange mandates traders to log in manually at least once a day, and we do not recommend automating the login process.

    Here are the key details:
    - Access tokens are flushed daily at 7:30 AM, and new tokens are generated at 7:35 AM.
    - Ensure you generate the access token after 7:35 AM on the same trading day. The token will remain valid throughout the day unless you log out.

    For more details, you can refer to the documentation for the login flow or check a similar discussion here.​
Sign In or Register to comment.