Its failing in authentication. "Invalid session credentials" error message is thrown. My login id is [email protected]. I am giving the right api key and the token as given in the website. But it fails with invalid session credentials error. I can't share my credentials on this forum. Could you please look into this? I am running the sample program Test to test the connectivity.
Hi @avinashsringeri, Please make sure you are not doing the following, 1. Use the same client id with which you have created the app. 2. Request token validity is for 5 min and it can only be used only once. So once you get access token store it and use the same, don't call requestAccessToken() every time you run app.
This is the sample code I am executing. In the kite trade site I got API key and the secret. I am using those values in this code to connect.
// First you should get request_token, public_token using kitconnect login and then use request_token, public_token, api_secret to make any kiteconnect api call. // Initialize KiteSdk with your apiKey. KiteConnect kiteconnect = new KiteConnect("api_key");
//set proxy is optional, if you want to set proxy. // kiteconnect.setProxy(new HttpHost("host_name"))
// Get login url String url = kiteconnect.getLoginUrl();
// Set session expiry callback. kiteconnect.registerHook(new SessionExpiryHook() { @Override public void sessionExpired() { System.out.println("session expired"); } });
// Set request token and public token which are obtained from login process. UserModel userModel = kiteconnect.requestAccessToken("api_key", "secret"); System.out.println(userModel.accessToken); System.out.println(userModel.publicToken); kiteconnect.setAccessToken(userModel.accessToken); kiteconnect.setPublicToken(userModel.publicToken);
Examples examples = new Examples(); examples.getMargins(kiteconnect);
Its working. The sample code with class name "Test" given as part of Kite Connect is very misleading. The other users could also experience the same problem as I did. This sample code should have an request_token as an mandatory input parameter or at runtime this code should get request token and move on with other operations.
Hanan thanks for your call. Thanks for all the help I received from the support group.
Please make sure you are not doing the following,
1. Use the same client id with which you have created the app.
2. Request token validity is for 5 min and it can only be used only once. So once you get access token store it and use the same, don't call requestAccessToken() every time you run app.
// First you should get request_token, public_token using kitconnect login and then use request_token, public_token, api_secret to make any kiteconnect api call.
// Initialize KiteSdk with your apiKey.
KiteConnect kiteconnect = new KiteConnect("api_key");
// set userId
// kiteconnect.setUserId("[email protected]");
kiteconnect.setUserId("[email protected]");
//set proxy is optional, if you want to set proxy.
// kiteconnect.setProxy(new HttpHost("host_name"))
// Get login url
String url = kiteconnect.getLoginUrl();
// Set session expiry callback.
kiteconnect.registerHook(new SessionExpiryHook() {
@Override
public void sessionExpired() {
System.out.println("session expired");
}
});
// Set request token and public token which are obtained from login process.
UserModel userModel = kiteconnect.requestAccessToken("api_key", "secret");
System.out.println(userModel.accessToken);
System.out.println(userModel.publicToken);
kiteconnect.setAccessToken(userModel.accessToken);
kiteconnect.setPublicToken(userModel.publicToken);
Examples examples = new Examples();
examples.getMargins(kiteconnect);
examples.placeOrder(kiteconnect);
examples.placeBracketOrder(kiteconnect);
examples.getTriggerRange(kiteconnect);
examples.placeCoverOrder(kiteconnect);
examples.getOrders(kiteconnect);
examples.getTrades(kiteconnect);
examples.getTradesWithOrderId(kiteconnect);
examples.modifyOrder(kiteconnect);
examples.cancelOrder(kiteconnect);
examples.getPositions(kiteconnect);
examples.getHoldings(kiteconnect);
examples.modifyProduct(kiteconnect);
examples.getAllInstruments(kiteconnect);
examples.getInstrumentsForExchange(kiteconnect);
examples.getQuote(kiteconnect);
examples.getHistoricalData(kiteconnect);
examples.logout(kiteconnect);
examples.tickerUsage(kiteconnect);
kiteconnect.setUserId must be your zerodha client id.
Client id with which you want to trade.
This sample code should have an request_token as an mandatory input parameter or at runtime this code should get request token and move on with other operations.
Hanan thanks for your call. Thanks for all the help I received from the support group.