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.
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.
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
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
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.
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.
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!
Refer this document for complete flow.
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
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
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
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.
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.
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!