# How get a access token in m2m actions?

**URL:** <https://community.auth0.com/t/how-get-a-access-token-in-m2m-actions/137929>\
**Category:** Get Help\
**Tags:** ticket-management, support-center\
**Created:** [July 2, 2024, 3:01pm UTC](https://community.auth0.com/t/how-get-a-access-token-in-m2m-actions/137929 "2024-07-02T15:01:38Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![pmoraga](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/pmoraga/32/49049_2.png) [@pmoraga](https://community.auth0.com/u/pmoraga)\
**Post date:** [July 9, 2024, 3:26pm UTC](https://community.auth0.com/t/how-get-a-access-token-in-m2m-actions/137929/5 "2024-07-09T15:26:46Z")

</div>

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?

 ![Captura de pantalla 2024-07-09 a la(s) 11.25.16 a. m.](https://global.discourse-cdn.com/auth0/original/3X/7/a/7ace2f44f331bee52c43a3ed9ef58e625013d181.png)

my code is:

```auto
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

> [@Caching M2M access token in Actions](https://community.auth0.com/t/caching-m2m-access-token-in-actions/100734/3):
>
> Hi @saiful1991, Welcome to the Auth0 Community! I have tested this myself and found that the Access Token value is too large, which is why you are experiencing the undefined error. This is consistent with our [Actions Limitations](https://auth0.com/docs/customize/actions/limitations#:~:text=Cache%20keys%20have%20a%20maximum%20size%20of%2064%20bytes%20and%20values%20have%20a%20maximum%20size%20of%202048%20bytes) documentation, which states that Cache keys have a maximum size of 64 bytes and values have a maximum size of 2048 bytes. Investigating further, I found that the Access Token is 4234 bytes which exceed the maximum size allowed for a value. I am going to look further…

---

_[View the full topic](https://community.auth0.com/t/how-get-a-access-token-in-m2m-actions/137929)._
