# Auth0 Node Generate Access Token

**URL:** <https://community.auth0.com/t/auth0-node-generate-access-token/106309>\
**Category:** Get Help\
**Tags:** clients, management-api\
**Created:** [May 6, 2023, 12:31pm UTC](https://community.auth0.com/t/auth0-node-generate-access-token/106309 "2023-05-06T12:31:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cagataysayg](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cagataysayg/32/46960_2.png) [@cagataysayg](https://community.auth0.com/u/cagataysayg)\
**Post date:** [May 6, 2023, 12:31pm UTC](https://community.auth0.com/t/auth0-node-generate-access-token/106309/1 "2023-05-06T12:31:25Z")

</div>

Hello everyone,

I’m trying to generate access token to access Management Api with auth0(node).

When I’m generating my token through api explorer I can access to API. But when I’m generating manually it just not working.

My flow  
 → Generate access\_token calling `https://${domain}/oauth/token`  
 → Create a new management client with the access\_token  
 → Get users by management client.

import { ManagementClient } from “auth0”;  
import fse from “fs-extra”;  
import dotenv from “dotenv”;  
import axios from “axios”;  
import qs from “qs”;  
dotenv.config();

const tokenPath = `${__dirname}../../../.auth0`;  
const domain = process.env.AUTH0\_DOMAIN || “”;  
const clientId = process.env.AUTH0\_CLIENT\_ID;  
const clientSecret = process.env.AUTH0\_CLIENT\_SECRET;  
const managementClientId = process.env.AUTH0\_MANAGEMENT\_CLIENT\_ID;  
const managementClientSecret = process.env.AUTH0\_MANAGEMENT\_CLIENT\_SECRET;

const createAuth0ManagementToken = async () =\> {  
const body = qs.stringify({  
grant\_type: “client\_credentials”,  
client\_id: managementClientId,  
client\_secret: managementClientSecret,  
audience: `https://${domain}/api/v2/`,  
});

const config = {  
method: “post”,  
url: `https://${domain}/oauth/token`,  
headers: {  
“content-type”: “application/x-www-form-urlencoded”,  
},  
data: body,  
};

const { data } = await axios.request(config);  
return data;  
};

export const auth0Management = async () =\> {  
let token = await fse.readJSON(`${tokenPath}/token.json`).catch(() =\> null);

if (!token) {  
token = await createAuth0ManagementToken();

```
const dir = await fse.readdir(tokenPath).catch(() => null);
if (!dir) await fse.mkdir(tokenPath);

await fse.writeJSON(`${tokenPath}/token.json`, token);

```

}

const managementClient = new ManagementClient({  
domain,  
clientId,  
clientSecret,  
token: token.access\_token,  
});

console.log(await managementClient.getUsers());

return managementClient;  
};

auth0Management();

---

<div class="post-metadata">

**Author:** ![phi1ipp](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/phi1ipp/32/40616_2.png) [@phi1ipp](https://community.auth0.com/u/phi1ipp)\
**Post date:** [May 6, 2023, 4:33pm UTC](https://community.auth0.com/t/auth0-node-generate-access-token/106309/2 "2023-05-06T16:33:52Z")

</div>

Hm, why do you need to obtain the token manually? As long as you provide your client\_id and client\_secret SDK should do that for you, unless I’m not getting what you are trying to achieve.

This is how I did instantiated the client and called management API

```auto
const CLIENT_ID = 'client_id_of_the_app'
const CLIENT_SECRET = 'client_secret_of_the_app'
const AUTH0_DOMAIN = 'auth0_domain'; 
const AUTH0_BASE_URL = 'https://' + AUTH0_DOMAIN
const AUTH0_API_AUDIENCE = AUTH0_BASE_URL + '/api/v2/'

const auth0 = new ManagementClient({
    domain: AUTH0_DOMAIN,
    clientId:CLIENT_ID,
    clientSecret: CLIENT_SECRET,
    audience: AUTH0_API_AUDIENCE
});

...
app.post('/post', async (req, res) => {
    const { email, user_id, redirect_uri } = req.body;

    console.log('>> /post with ', inspect(req.body))

    const user = await auth0.users.get({ id: user_id })
    console.log('user: ' + inspect(user))

    await auth0.users.update({id: user_id}, {email}),

```

---

<div class="post-metadata">

**Author:** ![cagataysayg](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/cagataysayg/32/46960_2.png) [@cagataysayg](https://community.auth0.com/u/cagataysayg)\
**Post date:** [May 7, 2023, 1:31pm UTC](https://community.auth0.com/t/auth0-node-generate-access-token/106309/3 "2023-05-07T13:31:30Z")

</div>

Thanks for the solution, my actual problem was using react sdk on front-end but its a single page application so I wasn’t able to use that application for management api. I created a machine to machine app and grant the credentials and it worked like a charm.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 8, 2023, 11:20am UTC](https://community.auth0.com/t/auth0-node-generate-access-token/106309/4 "2023-05-08T11:20:31Z")

</div>

Teamwork makes the dream work!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [May 22, 2023, 11:21am UTC](https://community.auth0.com/t/auth0-node-generate-access-token/106309/5 "2023-05-22T11:21:11Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
