# Using Custom API with Permission getting "Unauthorized"

**URL:** <https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591>\
**Category:** Get Help\
**Tags:** users, management-api\
**Created:** [January 24, 2024, 3:15pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591 "2024-01-24T15:15:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerarca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gerarca/32/52802_2.png) [@Gerarca](https://community.auth0.com/u/Gerarca)\
**Post date:** [January 24, 2024, 3:15pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/1 "2024-01-24T15:15:33Z")

</div>

Hello! I created one API and I added 2 Permission, this is the configure:

 ![1](https://global.discourse-cdn.com/auth0/original/3X/7/b/7b23b1ed1e2e7b70bce96e4a5b5641f787c808b7.png)

and I enable to my aplication type machine to machine:

 ![2](https://global.discourse-cdn.com/auth0/original/3X/8/7/8760bcd888d8c74f47c1ec9f7028700d1377619f.png)

so, each time that I try to update some date I got this:

```auto
1. error: "Unauthorized"
2. message: "Bad audience: https://dev-app/api/v1"
3. statusCode: 401

```

why?? what’s wrong

this’s the endpoint that I’m using:

scope: `update:current_user_metadata`  
endpoint: [PATCH /api/v2/users/{id}]

I was checking the token that I’m sending through endpoint, I checked it with [jwt.io](https://jwt.io/) and the PAYLOAD:DATA is right

so I check too the grant\_types of my application type machine to Machine:

 ![3](https://global.discourse-cdn.com/auth0/original/3X/f/9/f9503514ecd29beb1b56f13c0b16dc437c87826c.png)

is right too

---

<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:** [January 24, 2024, 4:15pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/3 "2024-01-24T16:15:06Z")

</div>

Hey there @Gerarca!

> [@Gerarca](#):
>
> this’s the endpoint that I’m using:
> 
> scope: `update:current_user_metadata`  
> endpoint: [PATCH /api/v2/users/{id}]

Judging by the error, the `audience` param you are using to authorize (and subsequently the audience claim in your access token) is different than the audience of the endpoint you are attempting to use the access token against.

If you are indeed attempting to `PATCH` users at `/api/v2/users` then you will need to be sure you are passing a [Management API access token](https://auth0.com/docs/secure/tokens/access-tokens/management-api-access-tokens) in the request. This access token will need to have an audience of `https://{yourDomain}/api/v2/`

[https://community.auth0.com/t/what-is-the-audience/71414](https://community.auth0.com/t/what-is-the-audience/71414)

---

<div class="post-metadata">

**Author:** ![Gerarca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gerarca/32/52802_2.png) [@Gerarca](https://community.auth0.com/u/Gerarca)\
**Post date:** [January 24, 2024, 6:17pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/4 "2024-01-24T18:17:08Z")

</div>

Hello @ty.frith

I’m working with a SPA, so checking [Get Management API Access Tokens for Single-Page Applications](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-tokens-for-single-page-applications) I see:

```auto
Password changes through the PATCH /api/v2/users/{id} endpoint are not possible with a Management API Token issued for a SPA.

```

that is so sad 🥲

what is the better way to change the password??

---

<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:** [January 24, 2024, 11:08pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/5 "2024-01-24T23:08:08Z")

</div>

That is correct, Management API Tokens obtained directly through a SPA are limited in scope by design.

> [@Gerarca](#):
>
> what is the better way to change the password??

The most common way of going about this is using a backend to serve as proxy for the SPA making calls against the management API:

[https://community.auth0.com/t/how-can-i-enable-users-to-change-their-email-address-from-a-spa-or-native-app/44064](https://community.auth0.com/t/how-can-i-enable-users-to-change-their-email-address-from-a-spa-or-native-app/44064)

If you’re interested, here is an [example](https://github.com/tyfrth/auth0-react-samples-proxy/blob/d12c61173852fabe743d7b5b9f417b81799442a6/Sample-01/api-server.js#L56) of what this might look like in a Node backend - This extends our standard auth0-react [sample app](https://github.com/auth0-samples/auth0-react-samples/tree/master).

---

<div class="post-metadata">

**Author:** ![Gerarca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/gerarca/32/52802_2.png) [@Gerarca](https://community.auth0.com/u/Gerarca)\
**Post date:** [January 26, 2024, 1:16pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/6 "2024-01-26T13:16:58Z")

</div>

Thanks @ty.frith

Worked it for me!

---

<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:** [January 26, 2024, 4:29pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/7 "2024-01-26T16:29:32Z")

</div>

No problem, happy to help! Glad you were able to get it working 🙂

---

<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:** [February 9, 2024, 4:30pm UTC](https://community.auth0.com/t/using-custom-api-with-permission-getting-unauthorized/125591/8 "2024-02-09T16:30:18Z")

</div>

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