How get a access token in m2m actions?

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?

Reference:

Hi @pmoraga,

Welcome to the Auth0 Community!

I have just tested this on my end and was able to call the await management.getAccessToken() method successfully.

Have you made sure to include the required dependencies in your Action script? Specifically, the Auth0 NPM module?

Thanks,
Rueben

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?

my code is:

exports.onExecuteCredentialsExchange = async (event, api) => {
      const ManagementClient = require('auth0').ManagementClient;
      const jwt_decode = require('jwt-decode');

      //get current value for 'my-api-token'
      const record = api.cache.get('management-token');
      console.log("Record: ", record);
      let token = record?.value;
      let current_time = Date.now().valueOf() / 1000;

      console.log("Cached token: ", token);
      if (token != undefined) {
        var decoded = jwt_decode(token);
      }

      //Check if cached token exists/is not undefined and not expired
      if (token != undefined && decoded.exp > current_time) {
        //Initialize management client with existing token to use against Management API
        const managementWithOldToken = new ManagementClient({
          token: token,
          domain: event.secrets.DOMAIN,
        });
      
      } 
      else if (token == undefined || decoded.exp < current_time) {
        //Initialize management client with credentials
        const management = new ManagementClient({
          domain: event.secrets.DOMAIN,
          clientId: event.request.body.client_id,
          clientSecret: event.request.body.client_secret,
          audience: event.request.body.audience,
          tokenProvider: {
            enableCache: true,
            cacheTTLInSeconds: 86400
          }
        });

        //Get new access token and set it in cache
        const newToken = await management.getAccessToken();
        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
        //Initialize management client with new token to use against Management API
        const managementWithNewToken = new ManagementClient({
          token: getNewToken,
          domain: event.secrets.DOMAIN,
        });      
        console.log('token success' , getNewToken)
      }
}

i saw this example @rueben.tiow