I am developing a web application in which I interact with the ebay API. I need to get user policies through API requests. What I have done is create a method for each one, in this case there would be 3 methods, and finally one that calls all of them through "request/request-promise". The thing is that to make those calls I need an authorization token which I can already obtain. Before making the calls to the 3 endpoints from which I am going to obtain the policies, there is another function with which I check if the token has expired, which it does every two hours, and as a consequence you have to use the "refresh_token" to do another request to the ebay API to refresh that token. The case is that when I don't have to refresh the token, the 3 calls are made completely normally, but if it needs to be refreshed, it only makes the call to the token refresh endpoint and then it does not continue, it enters the policy call functions, but in the first one it stops already. The thing is that when I consult the POSTMAN console I get the ECONNRESET error and the empty headers.
Here the calls are made to check the token and then to the rest:
async function getAllPolicies(req, res, next) {
try {
/*Más codigo, a contiación se realizan las llamadas*/
await checkTokenExpiration(ebShop.creationDate, ebShop.expirationDate, shopName);
let returnPolicies = await requestP(baseUrl + `getReturnPolicies/${shopName}`);
let returnPJSON = JSON.parse(returnPolicies);
let paymentPolicies = await requestP(baseUrl + `getPaymentPolicies/${shopName}`);
let paymentPJSON = JSON.parse(paymentPolicies);
let fulfillmentPolicies = await requestP(baseUrl + `getFulfillmentPolicies/${shopName}`);
let fulfillmentPJSON = JSON.parse(fulfillmentPolicies);
let allPolicies = { return: returnPJSON.returnPolicies, fulfillment: fulfillmentPJSON.fulfillmentPolicies, payment: paymentPJSON.paymentPolicies };
return res.status(200).send(allPolicies);
} catch (e) {
next(e);
}
}
This is where the token is checked:
async function checkTokenExpiration(creationDate, expirationDate, shopName) {
try {
let exp = moment(expirationDate);
let cre = moment(creationDate);
let diff = exp.diff(cre, 'minutes');
if (diff >= 120) {
console.log("El token ha expirado y debe ser renovado.");
await requestP.post(baseUrl + `/refreshEbayToken/${shopName}`);//Aquí se hace la llamda al endpoint con el que renuevo el token.
} else {
console.log(`Quedan ${120 - dif} minutos para que expire el token.`);
}
} catch (e) {
throw new Error(e);
}
}
Function that refreshes the token:
async function refreshToken(req, res, next) {
/*Aquí hay código pero irrelevante en lo que el problema se refiere*/
try {
let result = await requestP.post(reqOptions);
let resultJSON = JSON.parse(result);
let access_token = resultJSON.access_token;
let acces_token_expires = resultJSON.expires_in;
let date_token_exp = moment().add(acces_token_expires, 'seconds');
let dateToday = moment();
let shopsConfigFile = await utils.readShopsFile();
let shopFromFile = shopsConfigFile.filter(s => s.name == shopName)[0];
shopFromFile.oAuthToken = access_token;
shopFromFile.oAuthExpires = date_token_exp;
shopFromFile.creationDate = dateToday;
await utils.writeShopsFile(shopsConfigFile);
return res.send({ ack: true, result: result });
} catch (e) {
next(e);
}
}
After executing the refresh of the token, everything continues, until it enters the first method of obtaining policies and once the "request" is executed, it stops, which if there is no token to refresh, it does not happen.
I have already found where the error is generated. Being with "nodemon" active in the development environment, each change in any of the files that make up the application will be controlled by Nodemon, restarting the server. At one point in the process, I write to some files via the "File System" dependency promisifying both "writeFile" and "readFile". Then once it writes to the files the entire application is restarted. When Nodemon is not active everything goes as normal since there is nothing "watching" code changes.