my access token is not working not more than one time.

nivasdhina
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
session generated
{
user_type: 'individual',
email: '[email protected]',
user_name: 'Nivash',
user_shortname: 'Nivash',
broker: 'ZERODHA',
exchanges: [
'BFO', 'CDS',
'BSE', 'MCX',
'NFO', 'MF',
'NSE'
],
products: [ 'CNC', 'NRML', 'MIS', 'BO', 'CO' ],
order_types: [ 'MARKET', 'LIMIT', 'SL', 'SL-M' ],
avatar_url: '',
user_id: 'LT6932',
api_key: 'y3jjenmoixx0rwdm',
access_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
public_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
refresh_token: '',
silo: '',
login_time: 2020-11-11T02:51:10.000Z,
meta: { demat_consent: 'physical' }
}
{
equity: {
enabled: true,
net: 4102.79,
available: {
adhoc_margin: 0,
cash: 6352.79,
opening_balance: 6352.79,
live_balance: 4102.79,
collateral: 0,
intraday_payin: 0
},
utilised: {
debits: 2250,
exposure: 0,
m2m_realised: 0,
m2m_unrealised: 0,
option_premium: 2250,
payout: 0,
span: 0,
holding_sales: 0,
turnover: 0,
liquid_collateral: 0,
stock_collateral: 0,
delivery: 0
}
},
commodity: {
enabled: true,
net: 7412.857599999988,
available: {
adhoc_margin: 0,
cash: 156149.15,
opening_balance: 156149.15,
live_balance: 7412.857599999988,
collateral: 0,
intraday_payin: 0
},
utilised: {
debits: 148736.2924,
exposure: 0,
m2m_realised: 0,
m2m_unrealised: 0,
option_premium: 0,
payout: 0,
span: 0,
holding_sales: 0,
turnover: 0,
liquid_collateral: 0,
stock_collateral: 0,
delivery: 0
}
stock_collateral: 0,
delivery: 0
}
}
}

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.

E:\firstKiteConnectApp>
Tagged:
  • rakeshr
    @nivasdhina
    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.
  • nivasdhina
    nivasdhina edited November 12
    @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.
  • rakeshr
    @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.
  • nivasdhina
    iam just trying the sample code form the api




    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);
    });
    })();
  • sujith
    sujith edited November 13
    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.
Sign In or Register to comment.