# Calling Management API fails with "Bad Audience" even though audience matches Management API identifier

**URL:** <https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343>\
**Category:** Get Help\
**Created:** [March 31, 2021, 2:58am UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343 "2021-03-31T02:58:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackl](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackl/32/12805_2.png) [@jackl](https://community.auth0.com/u/jackl)\
**Post date:** [March 31, 2021, 2:58am UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/1 "2021-03-31T02:58:15Z")

</div>

I’m using a client credentials flow to access the Management API. I’ve configured the settings such that my M2M app is authorized to request access tokens for the Management API, and it has all available permissions. I’ve decoded the token at [jwt.io](http://jwt.io) and ensured it has all possible scopes, and the audience is the Management API identifier.

The issue appears to have something to do with the domain. My domain looks like this:  
`{tenant-name}.us.auth0.com`.

I’m performing the client-credentials token request using the audience: `https://{tenant-name}.us.auth0.com/api/v2/`

- When I make a GET request to `/api/v2/clients` I get a 400 error - bad request
- if I make the request to the same endpoint, **but removing ‘.us’ from the domain** , I get a 401:

```json
{
  statusCode: 401,
  error: 'Unauthorized',
  message: 'Bad audience: https://{tenant-name}.us.auth0.com/api/v2/'
}

```

Why am I getting nothing when I send a request to the correct url, but when I omit the ‘.us’ I do get a response, but the audience is bad?

```plaintext
import fetch from 'node-fetch'
  const endpoint = `https://${tenantName}.us.auth0.com/api/v2/clients`
  const options = {
    method: 'get',
    headers: {
      'content-type': 'application/json',
      Authorization: `Bearer ${AUTH0_MANAGEMENT_TOKEN}`
    },
  }
  const response: Response = await fetch(endpoint, options)
  console.log(response)
  const result = await response.json()
  console.log(result)

```

Proof the token has the “read:clients” scope:  
 ![image](https://global.discourse-cdn.com/auth0/original/3X/e/6/e6d7ef8912ef6e99c1f6c334f54fb25f2ad0bc65.png)

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 31, 2021, 3:25pm UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/2 "2021-03-31T15:25:40Z")

</div>

Hi @jackl,

Welcome to the Community!

If you go to your _APIs_ → _Auth0 Management API_ → _Test_, you should see an example request.

The 400 error typically means there is a problem with the request itself, like an issue with formatting or otherwise.

You can try using the [Management API Explorer](https://auth0.com/docs/api/management/v2#!/Clients/get_clients) to rule out any tenant configuration errors. This will also confirm that your token is correct.

---

<div class="post-metadata">

**Author:** ![jackl](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackl/32/12805_2.png) [@jackl](https://community.auth0.com/u/jackl)\
**Post date:** [March 31, 2021, 4:53pm UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/3 "2021-03-31T16:53:10Z")

</div>

@dan.woda Thanks for your response.

I tried doing it that way. The behavior is exactly the same.

If I send a request where the domain matches what’s in the audience, I get a 400 bad request. If I remove the ‘.us’ from the domain, I get `401 Unauthorized: Bad audience` (because the audience still has the ‘.us’ in it?).

Audience: `https://{tenant-name}.us.auth0.com/api/v2/`  
GET request to any API endpoint beginning with this ☝ returns `400 Bad Request`

GET request to any API endpoint beginning with _this_👉`https://{tenant-name}.auth0.com/api/v2/` returns

```auto
{
    "statusCode": 401,
    "error": "Unauthorized",
    "message": "Bad audience: https://{tenant-name}.us.auth0.com/api/v2/"
}

```

Have you ever heard of this behavior before?

---

<div class="post-metadata">

**Author:** ![jackl](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jackl/32/12805_2.png) [@jackl](https://community.auth0.com/u/jackl)\
**Post date:** [March 31, 2021, 5:07pm UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/4 "2021-03-31T17:07:01Z")

</div>

I found a solution, but I don’t understand the problem. My actions-flow added some custom claims to the token. When I removed the action, it worked as expected.

I don’t understand why having custom claims would affect the ability to access the Management API. If anyone can tell me what’s going on that would be helpful. Thanks!

**EDIT:**  
Originally, the action set the custom claims to include every scope as a key with “true” as the vaue. In this case there were **lots** of scopes, so there were many key-value pairs in the custom claims. I altered the action so there are less items in the custom namespace claims. Now it works again. Is there some documentation missing about how many claims are allowed? Or is this an issue worth investigating?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 31, 2021, 8:04pm UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/5 "2021-03-31T20:04:34Z")

</div>

Hmm it is hard to say. I don’t think there is a hard and fast limit on the number of custom claims, but it is possible that the token can get too big.

---

<div class="post-metadata">

**Author:** ![john.gateley](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/john.gateley/32/10879_2.png) [@john.gateley](https://community.auth0.com/u/john.gateley)\
**Post date:** [April 1, 2021, 1:06pm UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/6 "2021-04-01T13:06:50Z")

</div>

The access token is passed as a bearer token in an HTTP header. The standard doesn’t specify a size limit, but web browsers impose a limit. If the access token was larger than 8K, that probably was the issue.

John

---

<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 16, 2021, 1:07pm UTC](https://community.auth0.com/t/calling-management-api-fails-with-bad-audience-even-though-audience-matches-management-api-identifier/60343/7 "2021-04-16T13:07:05Z")

</div>

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