Now the token gets refreshed when necessary
This commit is contained in:
parent
8473880eb1
commit
fcd0b5f1ec
27
index.js
27
index.js
|
@ -6,12 +6,24 @@ const _accessTokenEndpoint =
|
|||
|
||||
import models from "./models/index.js";
|
||||
|
||||
const AccessToken = getClientToken();
|
||||
let AccessToken = await getClientToken();
|
||||
|
||||
let tokenValue = AccessToken[0];
|
||||
let tokenExpirationDate = AccessToken[1];
|
||||
|
||||
try {
|
||||
// Every 30 sec query the database
|
||||
setInterval(async () => {
|
||||
console.log("Querying the API to check for new data...");
|
||||
console.log("Current token expiration date: ", tokenExpirationDate);
|
||||
console.log("Now is: ", moment().format("YYYY-MM-DD HH:mm:ss"));
|
||||
|
||||
if(moment().isAfter(tokenExpirationDate)){
|
||||
console.log("Token expired, getting a new one...");
|
||||
AccessToken = await getClientToken();
|
||||
tokenValue = AccessToken[0];
|
||||
tokenExpirationDate = AccessToken[1];
|
||||
}
|
||||
|
||||
const query = models.tradeItem.findAll({
|
||||
include: [
|
||||
|
@ -92,7 +104,7 @@ try {
|
|||
const result = await query;
|
||||
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
}, 30000);
|
||||
}, 5000);
|
||||
} catch (error) {
|
||||
console.error("Unable to connect to the database:", error);
|
||||
}
|
||||
|
@ -104,14 +116,10 @@ async function getClientToken() {
|
|||
const now = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||
const tokenExpirationDate = clientConfigData[0].tokenExpiration;
|
||||
|
||||
console.log("tokenExpirationDate: ", tokenExpirationDate);
|
||||
|
||||
if (
|
||||
clientConfigData[0].tokenExpiration == null ||
|
||||
moment(now).isAfter(tokenExpirationDate)
|
||||
) {
|
||||
console.log("Getting a new token...");
|
||||
|
||||
let clientId = clientConfigData[0].clientId;
|
||||
let clientSecret = clientConfigData[0].clientSecret;
|
||||
|
||||
|
@ -131,9 +139,6 @@ async function getClientToken() {
|
|||
let tokenExpirationDate = moment(now)
|
||||
.add(tokenResponse.expires_in, "s")
|
||||
.format("YYYY-MM-DD HH:mm:ss");
|
||||
console.log(tokenResponse);
|
||||
console.log("now: ", now);
|
||||
console.log("tokenExpirationDate: ", tokenExpirationDate);
|
||||
|
||||
updateClientConfig(
|
||||
clientId,
|
||||
|
@ -142,10 +147,10 @@ async function getClientToken() {
|
|||
tokenExpirationDate
|
||||
);
|
||||
|
||||
return accessToken;
|
||||
return [accessToken, tokenExpirationDate];
|
||||
} else {
|
||||
console.log("Using the current token...");
|
||||
return clientConfigData[0].currentToken;
|
||||
return [clientConfigData[0].currentToken, tokenExpirationDate];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue