Could not get access token

madan
Hi, This is my code to get access token

KiteConnect kiteConnect = new KiteConnect("apikey");
kiteConnect.setUserId("userid");
kiteConnect.registerHook(new SessionExpiryHook() {
@Override
public void sessionExpired() {
System.out.println("session expired");
}
});
// got request token from login url
UserModel userModel = kiteConnect.requestAccessToken("request_token", "api_secret");


Getting exception : javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
In my previous discussion it was suggested to modify KiteTicker and use NaiveSSLContext. But I dont understand how KiteTicker is linked to above code.

I have also tried this way,

KiteConnect kiteConnect = new KiteConnect("apikey");
kiteConnect.setUserId("userid");
KiteTicker kiteTicker = new KiteTicker(kiteConnect);
kiteTicker.connect();

But even this is unsuccessful, looking at code it is expecting 'public_token'.

Please guide in getting access token.

I am just starting with kite trading.

Thank you.
Tagged:
  • madan
    Finally working with below code,

    KiteConnect kiteConnect = new KiteConnect("apikey");
    kiteConnect.setUserId("userid");
    kiteConnect.setPublicToken("requestToken");
    KiteTicker kiteTicker = new KiteTicker(kiteConnect); (Modified KiteTicker to use NaiveSSLContext)
    kiteTicker.connect();
    UserModel userModel = kiteConnect.requestAccessToken("request_token", "api_secret");
    kiteConnect.setAccessToken(userModel.accessToken);
    kiteConnect.setPublicToken(userModel.publicToken);

    //testing
    Margins margins = kiteconnect.getMargins("equity");
  • sujith
    Hi @madan,
    Adding NaiveSSLContext will only solve the issue with KiteTicker only.
    If you are facing this issue for HTTP calls then I think you need to update your system's trust anchor.
    The issue is your system trust anchor might not recognize our certificate issuer as trusted authority.
  • madan
    Yeah I have used NaiveSSLContext and It was working fine till yesterday.
    But from today morning I am getting Invalid "Invalid session credentials" even after giving fresh request token.

    Please suggest.
  • sujith
    Hi @madan,
    It is a bad practice to use request_token instead of public token for websockets.

    The request token is valid for a couple of minutes and it can be used only once. Once you get the request token, get the access token and store it and re-use it. Don't call requestAccessToken() everytime you run the app.
    The access token is valid for one whole day.
    If you log in to Pi or NEST, then you will be logged out of Kite Connect and I would suggest using Kite for monitoring purpose.
    If you log out of Kite then you are logged out everywhere.
Sign In or Register to comment.