I'm trying to refresh an oAuth token using axios from nodejs, the problem I'm getting is Invalid_grant, I've tried multiple ways but I can't refresh it.
When I need to make a call to the API this function acts as an intermediary and verifies if the token is active or not, in case it is not active it calls another function that is the one that gives me the problem.
async _verifyToken(req, res, next) {
let infoToken = await axios.get('https://xxxxxxxx/oauth/token/info', {
headers: {
Authorization: `Bearer ${req.session.token}`
}
});
if (infoToken.data['expires_in_seconds'] > 9000) {
console.log(clc.green(`Info: The token of the user ${req.session.login} is valid`));
return;
}
else {
console.log(clc.red(`Error: The token of the user ${req.session.login} is expired`));
req.session.token = await refreshAccessToken(req.session.token);
req.session.save();
console.log(clc.green(`Info: The token of the user ${req.session.login} has been refreshed`));
next();
}
}
This function below, is in charge of refreshing the token, I have searched the internet and the parameters it requires are: id,secret,uri and the old token.
refreshAccessToken = async (refreshToken) => {
const params = {
grant_type: 'refresh_token',
scope: 'public',
client_id: process.env.UID,
client_secret: process.env.SECRET,
redirect_uri: process.env.REDIRECT_URI,
refresh_token: refreshToken
};
try {
const response = await axios.post('https://xxxxxxxx/oauth/token', params);
console.log('Token refreshed' + response);
return;
}
catch (error) {
console.error(error);
}
}
when I run that process, the error I get is the following:
data: {
error: 'invalid_grant',
error_description: 'The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.'
Please help.