Token is invalid or has expired

atibrewal
Hi,
I am developing a kite3 client using python3 api binding. kitconnect is installed via pip3.

I opened a webbrowser from my python code, and after getting the redirect url, extract the request_token and call generate_session.



I always get "Token is invalid or has expired". The same response is seen via curl command line also.

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/1.1" 403 None
Traceback (most recent call last):
File "init.py", line 72, in
data = kite.generate_session(request_token=request_token,api_secret=apisecret)
File "/home/ankur/.local/lib/python3.7/site-packages/kiteconnect-3.8.0-py3.7.egg/kiteconnect/connect.py", line 252, in generate_session
File "/home/ankur/.local/lib/python3.7/site-packages/kiteconnect-3.8.0-py3.7.egg/kiteconnect/connect.py", line 818, in _post
File "/home/ankur/.local/lib/python3.7/site-packages/kiteconnect-3.8.0-py3.7.egg/kiteconnect/connect.py", line 883, in _request
kiteconnect.exceptions.TokenException: Token is invalid or has expired.


Can someone please help here ?
  • sujith
    You can check out to this thread.
  • atibrewal
    @sujith I checked this thread, and I am following the same.
    I am not getting the access_token because generate_session throws an exception.
    Can you please help in proceeding forward on this ?
  • rakeshr
    @atibrewal
    What is the exception?
    Are you assigning correct access_token and secret_key in generate_session?
    Refer this doc to know, about the complete authentication flow.
  • atibrewal
    @rakeshr The exception is token invalid or expired.

    Yes, I have checked this document. The parameters are the received request_token and api_secret_key

    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.kite.trade:443
    DEBUG:urllib3.connectionpool:https://api.kite.trade:443 "POST /session/token HTTP/1.1" 403 None
    Traceback (most recent call last):
    File "init.py", line 72, in
    data = kite.generate_session(request_token=request_token,api_secret=apisecret)
    File "/home/ankur/.local/lib/python3.7/site-packages/kiteconnect-3.8.0-py3.7.egg/kiteconnect/connect.py", line 252, in generate_session
    File "/home/ankur/.local/lib/python3.7/site-packages/kiteconnect-3.8.0-py3.7.egg/kiteconnect/connect.py", line 818, in _post
    File "/home/ankur/.local/lib/python3.7/site-packages/kiteconnect-3.8.0-py3.7.egg/kiteconnect/connect.py", line 883, in _request
    kiteconnect.exceptions.TokenException: Token is invalid or has expired.
  • rakeshr
    @atibrewal
    Above Token Exception comes only in case of wrong request_token or secret_key.Re-check the same.
  • atibrewal
    @rakeshr @sujith
    Turns out there was a new line character in both the apisecret and apikey. After removing it the problem is resolved.
    Sorry for the trouble, and many thanks for your help :smile:
This discussion has been closed.