Kite API Logging out after few requests

kris123
Hi,
I am trying to use the Kite API for data processing and i am not able to fetch the data from the API.
The API Keeps logging out no matter what, and am forced to generate request_token again.

Once i hit the API, i can get the list of Instruments and other details, but from second request or third request, the API logs out and throws an API Error.

Attaching a piece of code using the Kite API Examples. Exactly after 3/4 requests, i see that the session gets signed out and i have to regenerate the request token, however as per documentation, the request token is valid for one full day. [I am not invoking any Logout methods in my code, hence the request token should be valid for one full day.]

{
KiteConnect kiteConnect = new KiteConnect(apikey);
kiteConnect.setUserId("VH7775");
String url = kiteConnect.getLoginURL();

// Set session expiry callback.
kiteConnect.setSessionExpiryHook(new SessionExpiryHook() {
@Override
public void sessionExpired() {
System.out.println("session expired");
}
});

User user = null;
user = kiteConnect.generateSession(reqToken, apisec);
kiteConnect.setAccessToken(user.accessToken);
kiteConnect.setPublicToken(user.publicToken);

user = kiteConnect.generateSession(reqToken, apisec);

kiteConnect.setAccessToken(user.accessToken);
kiteConnect.setPublicToken(user.publicToken);

examples.getMargins(kiteConnect);
examples.getMarginCalculation(kiteConnect);
examples.getHistoricalData(kiteConnect);
examples.getOrders(kiteConnect);
examples.getOrder(kiteConnect);
examples.getTradesWithOrderId(kiteConnect);
examples.getPositions(kiteConnect);
examples.getHoldings(kiteConnect);
examples.getAllInstruments(kiteConnect);
}


Should i Generate and set access token after hitting few requests? if yes, after how many requests?

I have read somewhere that one of the token needs to be generated after hitting few request. Which one is it? Request you to kindly help in getting this issue resolved. Thank you.
  • MAG
    User user = null;
    user = kiteConnect.generateSession(reqToken, apisec);
    kiteConnect.setAccessToken(user.accessToken);
    kiteConnect.setPublicToken(user.publicToken);

    user = kiteConnect.generateSession(reqToken, apisec);

    kiteConnect.setAccessToken(user.accessToken);
    kiteConnect.setPublicToken(user.publicToken);


    This looks wrong. Why are you calling setAccessToken twice?

    Also, your initial flow of generating the access token should happen only once a day in a separate process.
    Once the access token is generated, it should be saved somewhere (whether txt file or db) and then referenced from that saved location.
    You should not be going through the token generation flow multiple times a day or every time you run your code.

    What is probably happening in your case is that you end up going through the access token generating flow, creating a new token. And that invalidates any previous access token created. And any running script using the previous access token will throw invalid access token exception and exit.
  • kris123
    @MAG , Thanks for your response.

    It was a typo, while pasting the code to this forum.

    The login process is executed only once, and the API works for few requests, but starting from 4th or 5th request, it starts throwing token exception. Should i have to set any token after hitting couple of requests? If not, what could be wrong.

    Also, could you please share a sample code of hitting few instruments historical data in a loop and share it, so i can cross check whats going wrong or whats being missed. Thank you.

Sign In or Register to comment.