Getting 'Token is invalid or has expired.' every time

sushant_tavaga
sushant_tavaga edited June 2018 in API clients
Here is my code:

var KiteConnect = require("kiteconnect").KiteConnect;
var kc = new KiteConnect({ api_key: "xxxxxxxxxxxxxxxx" }); // (16 digit api_key)

........
    console.log("zerodhaLogin - start");
console.log("request_token = ");
console.log(request_token);

if (request_token == null) {
console.log("Invalid request token");
cb("Invalid request token");
}

kc.generateSession(request_token, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") // (32 digit app secret)
.then(function (response) {
console.log("KC::generateSession Response == ");
console.log(response)
init();
})
.catch(function (err) {
console.log("Error in KC:generateSession : ERROR = "); //// Always getting here with error 'Token is invalid or has expired'
console.log(err);
cb(null, err);
}).finally(function (e) {
// On finish
console.log("KC:: -- finally: -- e == ");
console.log(e)
});

function init() {
// Fetch equity margins.
// You can have other api calls here.

kc.getMargins("equity")
.then(function (response) {
console.log("KC::Got Your margins: -- Response == ");
console.log(response)
// You got user's margin details.
cb(null, "THANK YOU");

}).catch(function (err) {
// Something went wrong.
console.log("KC:: -- ERROR in Getting Your margins: -- ERROR == ");
console.log(err)
cb(null, err);
}).finally(function (e) {
// On finish
console.log("KC:: -- finally: -- e == ");
console.log(e)
});
}
.......

I am getting correct request_token on my server but still 'generateSession' API call fails with error: 'Token is invalid or has expired'

Below is the output on console:

0|server | zerodhaLogin - start
0|server | request_token =
0|server | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // 32 digit request_token from redirect rest call
0|server | Error in KC:generateSession : ERROR =
0|server | { status: 'error',
0|server | message: 'Token is invalid or has expired.',
0|server | data: null,
0|server | error_type: 'TokenException' }
0|server | KC:: -- finally: -- e ==
0|server | undefined




Sign In or Register to comment.