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);
According to the 500 error code you experienced. it happens when your code takes more than 20 seconds to complete the action script. This is mentioned in our Actions Limitations documentation.
Additionally, I noticed you used the token already generated using the client credentials grant to initialize a new ManagementClient using the existing token. This is an unnecessary step since you already have a usable Management API token in your first request and can skip this step. This bit of code might be responsible for exceeding the execution time limit.
Lastly, I confirmed that Auth0 node version 3.7.2 is the last version that supports the getAccessToken() method. Newer versions do not support this method.
This behavior is expected since the cache item is not guaranteed for subsequent executions. It is only guarantees for all Actions in the same flow for a single execution reliably. This is mentioned in our Cached Data limitations documentation.