kiteconnect.exceptions.TokenException: Token is invalid or has expired

Naga
Hi , I have created latest request token and try create Kiteconnect session using correct API_KEY.
Access token and Secret key even though getting error like kiteconnect.exceptions.TokenException: Token is invalid or has expired.
FYI.., I am using ubuntu 21.10 and python 3.9.
Code:-

kite = KiteConnect(api_key=Jfile['Api_Key'])
#data = kite.generate_session(req_tocken, api_secret=Jfile['Api_Secret'])
gen_ss = kite.generate_session(request_token=req_tocken,api_secret=Jfile['Api_Secret'])
print(gen_ss["access_token"])

Error:-
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 "/home/Patter_detect/ZerodhaAlgoTr/kitecon.py", line 20, in
gen_ss = kite.generate_session(request_token=req_tocken,api_secret=Jfile['Api_Secret'])
File "/home/.local/lib/python3.9/site-packages/kiteconnect/connect.py", line 258, in generate_session
resp = self._post("api.token", params={
File "/home/arjun/.local/lib/python3.9/site-packages/kiteconnect/connect.py", line 841, in _post
return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json, query_params=query_params)
File "/home/.local/lib/python3.9/site-packages/kiteconnect/connect.py", line 913, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.TokenException: Token is invalid or has expired.

Please fix this issue.
  • SRIJAN
    SRIJAN edited April 21
    A request token is valid only for a few minutes and can be used only once. Try generating a new request token.
  • Naga
    I have generated five times and tested its not working.
  • SRIJAN
    SRIJAN edited April 21
    This shouldn't happen. Well,can you confirm that the line above the gen_ss ,where you declared 'data' variable is still a comment?? I can see a #. I mean you are not calling generate session twice,once when declaring 'data' and then 'gen_ss',right?? If you are not calling generate session twice,then only Sujith Sir,Rakesh Sir can help you.
  • Naga
    gen_ss and data both are same . I have commented data variable. please have look.
  • rakeshr
    You need to re-check if you are fetching the correct value from Jfile['Api_Secret'], it should be passed as string.
    You need to manually cross-verify data type(to be a string) and all values to be correctly passed for Jfile['Api_Key'], Jfile['Api_Secret'], and req_tocken.
  • Naga
    Naga edited April 22
    Hi @rakeshr ,

    I have printed the data type all are string.
    print(type(Jfile['Api_Key']))
    print(type(Jfile['Api_Secret']))
    print(type(req_tocken))

    op:-



    Please fix the issue.
  • Naga
    Hi @rakeshr ,

    I am able to get access token and able to pull data.
    Thank You!!
This discussion has been closed.