Session invalidation in kiteconnect api

rajtk
I am trying to download the list of available instruments using kiteconnect API. Below is the steps i followed.

1. Connect to the service via kiteconnect and obtain login_url

from manas.core.util import AppConfig
from kiteconnect import KiteConnect
api_key = AppConfig.configParser.get('login', 'api_key')
user_id = AppConfig.configParser.get('login', 'user_id')

kite = KiteConnect(api_key=api_key)
kite_login_url = kite.login_url()

2. After i get the public_token, i use the same to connect and download the instruments
from kiteconnect import KiteConnect
import os
from manas.core.util import AppConfig, Constants

# Connect to KITE API
kite = KiteConnect(api_key=AppConfig.configParser.get("login","api_key"))
data = kite.request_access_token(AppConfig.configParser.get("Authorisation","public_token"), secret=AppConfig.configParser.get("login","secret_key"))
kite.set_access_token(data["access_token"])

# Download all the instruments for NSE
instruments = kite.instruments(exchange=Constants.EXCHANGE_NSE)

3. When i again try to run the step 2 i.e download the instruments, i am getting session error as below.

Traceback (most recent call last):
File "/Users/vishravars/Desktop/kite/manas-core/src/manas/core/etl/DownloadInstruments.py", line 18, in
data = kite.request_access_token(AppConfig.configParser.get("Authorisation","public_token"), secret=AppConfig.configParser.get("login","secret_key"))
File "/Library/Python/2.7/site-packages/kiteconnect/__init__.py", line 235, in request_access_token
"checksum": checksum
File "/Library/Python/2.7/site-packages/kiteconnect/__init__.py", line 467, in _post
return self._request(route, "POST", params)
File "/Library/Python/2.7/site-packages/kiteconnect/__init__.py", line 554, in _request
raise(exp(data["message"], code=r.status_code))
kiteconnect.exceptions.TokenException: Invalid session credentials

Note: If i again login and use a new public_token, things start working.
Please let me know how to solve this problem of session invalidation. Because i do not experience the same when i use web sockets from kiteconnect.
  • sujith
    Hi @rajtk,
    Once you get access token it is valid until next day. The request token is valid for a couple of minutes and can be used only once.
    If you call request_access_token everytime you run script then it will give you token exception.
    So do call this API only once. Store the access token once you receive. Next time you run the script just check if the access token is already present then use if it exists.
  • rajtk
    Thanks
This discussion has been closed.