2

I am getting an access token from Flipkart API and gives me token with its expiry time . But i don't know what they given in expiry time they are giving some kind of timestamp in seconds, as i convert those seconds into minutes then there are 56 days like something that is coming. As i read the doc for that they said that token is expired after 60 days. But as i hit the API it will giving me different different time-stamp in expiry_time field each time. For example first timme it will give:-

"access_token" : "0c87d3c8-824d-446b-bqf8-c14e7d49f06",
"expires_in" : 4773358

After some time i hit it again then it returns:-

"access_token" : "0c87d3c8-824d-446b-bqf8-c14e7d49f06",
"expires_in" : 4840646

I just want to validate this token via its expiry time i.e. if token is expire i need to hit again and get fresh token.How can i check that token is expired or not.

How can i achieve this using java script or Meteor framework. Or i also want to know on which format they gives me this time-stamp in seconds i think or in some other.As they said that token is expired in 60 days but as i convert this it shows only 56 days not 60 so please explain this for me as well if somebody know.

Any help would be appreciated

Thanks!

Parveen yadav
  • 2,252
  • 1
  • 21
  • 35

2 Answers2

4

I suspect they are giving you a token that expires at midnight on a particular day of the week rather than 60243600 = 5,184,000 seconds from now. To convert this expiry delay to a specific datetime, do the following:

const expiryDate = Date.now() + expires_in * 1000

You have to multiply by 1000 because Date.now() returns milliseconds.

vovahost
  • 34,185
  • 17
  • 113
  • 116
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • 1
    can also use Date.now() and have it in one line: `(expiryDate = Date.now() + expires_in * 1000)` – ssdev Dec 12 '22 at 23:11
1

I solved it this way:

export const expiryToISODate = (expiresIn: number): string => {
  const now = new Date();
  now.setSeconds(now.getSeconds() + expiresIn);
  return now.toISOString();
};
ChrisRich
  • 8,300
  • 11
  • 48
  • 67