Hi, I have fetched an access token from the https://kite.zerodha.com/connect/login?api_key=xxx but I cannot use the access token which I fetch for more than one time. I work once perfectly and for the next time, it is saying that the access token is not valid or expired.
E:\firstKiteConnectApp>node index.js { status: 'error', message: 'Token is invalid or has expired.', data: null, error_type: 'TokenException' } (node:8124) UnhandledPromiseRejectionWarning: # (node:8124) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3) (node:8124) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
E:\firstKiteConnectApp>node index.js { status: 'error', message: 'Token is invalid or has expired.', data: null, error_type: 'TokenException' } (node:16272) UnhandledPromiseRejectionWarning: # (node:16272) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3) (node:16272) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
but I cannot use the access token which I fetch for more than one time
Are you re-generating access_token for each request? You need to generate an access token once, store it, and keep using the same access token to make a further request.
@rakeshr No I am not regenerating the access token... I am trying to use the same token again and I am getting this error. i knew that i can use it for 24hrs.
@nivasdhina Somewhere, you might be re-generating either access token or other user flow auth param. Can you paste your effecting throwing code? We will take look.
(()=> { // Fetch equity margins. // You can have other api calls here. kc.getMargins() .then(function(response) { // You got user's margin details. console.log(response); }).catch(function(err) { // Something went wrong. console.log(err); }); })();
for the next time, it is saying that the access token is not valid or expired.
When you run the same code(i.e index.js here) for next time, generateSession function here,kc.generateSession("request_token", "api_secret") tries to re-generate session with the old request_token, which is invalid now as it has expired(because it was used to generate access_token last time). You can take a look at this thread.
You need to generate an access token once, store it, and keep using the same access token to make a further request.
Somewhere, you might be re-generating either access token or other user flow auth param.
Can you paste your effecting throwing code? We will take look.
var kc = new KiteConnect({
api_key: "xxxxxxxxxxxxxxxxxx"
});
kc.generateSession("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.then(function(response) {
console.log("session generated",response);
})
.catch(function(err) {
console.log(err);
});
(()=> {
// Fetch equity margins.
// You can have other api calls here.
kc.getMargins()
.then(function(response) {
// You got user's margin details.
console.log(response);
}).catch(function(err) {
// Something went wrong.
console.log(err);
});
})();
generateSession
function here,kc.generateSession("request_token", "api_secret")
tries to re-generate session with the old request_token, which is invalid now as it has expired(because it was used to generate access_token last time).You can take a look at this thread.