Token invalid or expired

tsgoud69
tsgoud69 edited December 2020 in Python client
Hi All,

I am new to kiteconnect api. Using below code to authenticate kiteconnect api, however I am receiving Token invalid or expired error. Could you please advise.

######################### CODE ####################################
from kiteconnect import KiteConnect

api_key = open('api_key.txt', 'r').read()
api_secret = open('api_secret.txt', 'r').read()
kite = KiteConnect(api_key=api_key)

#print(kite.login_url())

obtained_request_token = "XXXXXX"
data = kite.generate_session(obtained_request_token, api_secret=api_secret)
####################### ERROR ########################################
#python kconnect.py
Traceback (most recent call last):
File "kconnect.py", line 11, in
data = kite.generate_session(obtained_request_token, api_secret=api_secret)
File "/home/tatis/anaconda3/envs/Trade/lib/python3.8/site-packages/kiteconnect/connect.py", line 252, in generate_session
resp = self._post("api.token", params={
File "/home/tatis/anaconda3/envs/Trade/lib/python3.8/site-packages/kiteconnect/connect.py", line 826, in _post
return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json)
File "/home/tatis/anaconda3/envs/Trade/lib/python3.8/site-packages/kiteconnect/connect.py", line 894, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.TokenException: Token is invalid or has expired.
  • rakeshr
    You can go through this thread.
  • tsgoud69
    @rakeshr Hi Rakesh,

    I have followed the thread, I understand the request token, I have opened the login.url() in browser and got the request token, but when I supplied the request token, I am getting the error.

    #python kconnect.py
    https://kite.trade/connect/login?api_key=XXXXXXXXXXX&v=3 ==> Opened this in browser and got request token

    ##################
    Here is how I received the request token in browser:
    http://127.0.0.1/?request_token=I2Ul3tsJtXSOYp0Lk6AK0BxdteNWTdLO&action=login&status=success
    ################

    Enter request token:I2Ul3tsJtXSOYp0Lk6AK0BxdteNWTdLO <= Supplied request token here, but failed

    Traceback (most recent call last):
    File "kconnect.py", line 10, in <module>
    data = kite.generate_session(obtained_request_token, api_secret=api_secret)
    File "/home/tatis/anaconda3/envs/Trade/lib/python3.8/site-packages/kiteconnect/connect.py", line 252, in generate_session
    resp = self._post("api.token", params={
    File "/home/tatis/anaconda3/envs/Trade/lib/python3.8/site-packages/kiteconnect/connect.py", line 826, in _post
    return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json)
    File "/home/tatis/anaconda3/envs/Trade/lib/python3.8/site-packages/kiteconnect/connect.py", line 894, in _request
    raise exp(data["message"], code=r.status_code)
    kiteconnect.exceptions.TokenException: Token is invalid or has expired.
  • sujith
    @tsgoud69
    You may refer to this thread.
Sign In or Register to comment.