# Node-auth0 doesn't automatically obtain token for the management api

**URL:** <https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [March 18, 2024, 10:40pm UTC](https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944 "2024-03-18T22:40:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mscafidimcguire](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mscafidimcguire/32/48577_2.png) [@mscafidimcguire](https://community.auth0.com/u/mscafidimcguire)\
**Post date:** [March 18, 2024, 10:40pm UTC](https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944/1 "2024-03-18T22:40:02Z")

</div>

I’m struggling to access the management api from a backend service running in node.

I’m trying to access the `Auth0 Management API` in a tenant. In the same tenant, I have an m2m application that has authorized that API. This m2m tenant is the test m2m application that was automatically created for a SPA application in the same tenant.

On the [Get Management API access tokens for production](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production) page, after showing how to manually get an access token with an HTTP call, it says:

“As an alternative to making HTTP calls, you can use the node-auth0 library to automatically obtain tokens for the Management API.”

To me, that sounds like the node-auth module will handle getting the token. Looking at it’s examples, it seems to show the same with an example to update a users metadata like this:

```auto
import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
});

await management.users.update({ id: '{user id}' }, { user_metadata: { foo: 'bar' } });

```

However when trying this example with the m2m application & management API as described I receive an `Unauthorized` response.

```auto
{"statusCode":401,"error":"Unauthorized","message":"Invalid token","attributes":{"error":"Invalid token"}}

```

It’s not clear if I have the m2m application misconfigured or if the node-auth0 documentation is wrong and I still need to manually request an access token before creating the ManagementClient. Or if it’s something else altogether.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 19, 2024, 12:11am UTC](https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944/3 "2024-03-19T00:11:48Z")

</div>

Hello @mscafidimcguire welcome to the community!

> [@mscafidimcguire](#):
>
> It’s not clear if I have the m2m application misconfigured or if the node-auth0 documentation is wrong and I still need to manually request an access token before creating the ManagementClient. Or if it’s something else altogether.

I believe the only thing absent is the audience param in your `ManagementClient` configuration:

```auto
import { ManagementClient } from 'auth0';

const management = new ManagementClient({
  domain: '{YOUR_TENANT_AND REGION}.auth0.com',
  clientId: '{YOUR_CLIENT_ID}',
  clientSecret: '{YOUR_CLIENT_SECRET}',
  audience: '{YOUR_DOMAIN}/API/V2/'

});

await management.users.update({ id: '{user id}' }, { user_metadata: { foo: 'bar' } });

```

Here’s an [example](https://github.com/tyfrth/auth0-react-samples-proxy/blob/d12c61173852fabe743d7b5b9f417b81799442a6/Sample-01/api-server.js#L41) utilizing a React sample app.

---

<div class="post-metadata">

**Author:** ![mscafidimcguire](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mscafidimcguire/32/48577_2.png) [@mscafidimcguire](https://community.auth0.com/u/mscafidimcguire)\
**Post date:** [March 19, 2024, 1:32pm UTC](https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944/4 "2024-03-19T13:32:10Z")

</div>

Thanks, I think I found my issue. I didn’t have the M2M application scopes configured correctly nor was I including the required scopes in my request.

Thanks

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [March 19, 2024, 9:38pm UTC](https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944/5 "2024-03-19T21:38:22Z")

</div>

Great! Glad you were able to get this sorted and thanks for following up here 🙂

---

<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:** [April 2, 2024, 9:38pm UTC](https://community.auth0.com/t/node-auth0-doesnt-automatically-obtain-token-for-the-management-api/129944/6 "2024-04-02T21:38:42Z")

</div>

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