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

akash_mittal
akash_mittal edited March 30 in Python client
Hi support,

I am getting the following exception when I am trying to use the "generate_session" API to generate the access token from the request token.

Stack Trace:---------------------------------------------------------------------------

Traceback (most recent call last):
File "C:\Users\akash\github\KiteConnectTrading\main.py", line 130, in
val = kite.generate_session(request_token,api_secret=api_secret)
File "C:\Users\akash\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 256, in generate_session
resp = self._post("api.token", params={
File "C:\Users\akash\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 830, in _post
return self._request(route, "POST", url_args=url_args, params=params, is_json=is_json)
File "C:\Users\akash\AppData\Local\Programs\Python\Python39\lib\site-packages\kiteconnect\connect.py", line 898, in _request
raise exp(data["message"], code=r.status_code)
kiteconnect.exceptions.TokenException: Token is invalid or has expired.
----------------------------------------------------------------------------------------------

Please help me resolve this issue as soon as possible.

Regards,
Akash
  • rakeshr
    @akash_mittal
    val = kite.generate_session(request_token,api_secret=api_secret)
    You need to re-check if you are using correct request_token and api_secret values here.
    Refer this document for complete flow.
  • akash_mittal
    Hi @rakeshr ,

    Thanks for the response.
    I have made sure that the api key and the api secret key I am using are correct.

    I am following these steps to generate the access token:

    1. create kite object using the api and api_key: "kite = KiteConnect(api_key=api_key)"
    2. Log in to the url as returned by kite.login_url()
    3. Copy the request_token from the redirected url
    4. Paste that request_token in the first argument of the api generate_session: "val = kite.generate_session(request_token,api_secret=api_secret)"
    5. Set the access_token as: "kite.set_access_token(val['access_token'])"
    6. Re-run the python file with the request_token.

    Please let me know if I am doing something wrong in the above mentioned steps, or something I might have missed.

    Regards,
    Akash
  • sujith
    You can check out this thread.
  • akash_mittal
    Hi @sujith ,

    I have already scanned the forum articles thoroughly to look for the solution and tried everything already mentioned, but to no avail. Please let me know if I am doing anything wrong in the above steps. I think that there might be some other issue in this.

    I am able to successfully generate the request token with the steps mentioned in the previous comment, but I am not able to understand why this request token is not being accepted by the system.
    This is what I get when I follow the URL generated by the kite.login_url():
    http://127.0.0.1/?action=login&;status=success&request_token=xxxxxxxxxxxxxxxxxxx

    Regards,
    Akash
  • sujith
    It can be used only once and has a validity of a couple of minutes only.
  • akash_mittal
    Hi @sujith

    I know that already. I am not able to generate the access token even once. I want to understand why the generate_session api is not able to accept the generated request token
  • sujith
    A request token can be used only once and it is only valid for a couple of minutes. If you try to re-use the invalid request token then you won't be able to generate a session.
    You need to store the generated access token and use that in the subsequent runs. That is what is mentioned in the above-linked thread.
  • akash_mittal
    Yes, I am generating a new request token for every run, and not using the previous token. I have not been able to generate the access token even once.
  • akash_mittal
    The problem is that I am not able to get past the "generate_session" step itself. The system is rejecting the freshly generated request token, because of some unknown reason.
  • sujith
    Can you give us the status code? Is it 400 or 403?
  • akash_mittal
    Where can I see the status code?
  • akash_mittal
    @sujith , can you please tell me how to retrieve the status code?
  • sujith
    You can run pykiteconnect in debug mode and see that response status code.
  • akash_mittal
    @sujith , the status code is 403
  • sujith
    It means the request token is expired or invalid.
  • akash_mittal
    @sujith, would you be able to help if I provide you with my api_key and api_secret? It would be a great help
  • rakeshr
    @akash_mittal
    Yes, you can DM us with api_key and api_secret, you are facing issue. Never paste your API-related keys and token on a public platform.
  • akash_mittal
    I have messaged you the keys. Please check
  • akash_mittal
    It worked!
    Apparently, the api_key and api_secret keys while reading from a text file were being appended with a \n. The generate_session api started working after i removed the \n.
    Thanks a lot for your help and support!
This discussion has been closed.