I was looking in previous questions but the answers did not work, my purpose is to get the access token in a m2m flow, I have seen in other answers that use " const newToken = await management.getAccessToken();" but it gives me error because getAccessToken does not exist in the class,
how can I access the machine’s access token in an action for an m2m flow?
Hi Ruben, thank for you response, i actually use this version (in image)
for other side, i have a cuestion for you ruben, in someones post of cummunity i saw what use api.cache.get(‘management-token’); for get token in cache, is correct that way?
I do downgrade the version of “auth0” to 3.7.2 and find the method getAccessToken, but i saw other solution your proposes and i take that code for test, but when i call the service give error for timeout, for other side is the same when i try connect with redis from auth0 action, its a capability of auth0 it can connect to extern service? or ¿i should do a config some?
I saw this example and the discussion for Caching the Access token with key. I am able to set the cache but while doing the next call again I not getting the cached data with same key.
my code
const record = api.cache.get(‘my-api-token’);
console.log(‘In function record’, record);
let token = record?.value;
let current_time = Date.now().valueOf() / 1000;
if (token != undefined) {
var decoded = jwtDecode(token);
}
console.log('existing token', token);
console.log('decoded', decoded);
console.log('current_time', current_time);
if (token != undefined && decoded.exp > current_time) {
console.log('if condition');
const managementWithOldToken = new ManagementClient({
token: token,
domain: event.secrets.domain,
});
return token;
//Call Management API
} else if (token == undefined || decoded.exp < current_time) {
console.log('else if condition');
//Initialize management client with credentials
const management = new ManagementClient({
domain: event.secrets.M2M_DOMAIN,
clientId: event.secrets.M2M_CLIENT_ID,
clientSecret: event.secrets.M2M_CLIENT_SECRET,
audience: event.secrets.AUDIENCE,
tokenProvider: {
enableCache: true,
cacheTTLInSeconds: 86400
}
});
//Get new access token and set it in cache
const newToken = await management.getAccessToken();
console.log('newToken', newToken);
console.log('newToken size', newToken?.size);
// api.cache.set('first',newToken.slice(0,2048))
// api.cache.set('second', newToken.slice(2048,4096))
// api.cache.set('third', newToken.slice(4096))
// const getNewToken = api.cache.get('first').value + api.cache.get('second').value + api.cache.get('third').value
// console.log('getNewToken', getNewToken);
const result = api.cache.set('my-api-token', newToken);