C# API: test token for management API returns AggregateException

This is giving me an error (forgive my F#):

#r "nuget: FSharpPlus"
#r "nuget: Auth0.Core"
#r "nuget: Auth0.ManagementApi"

open FSharpPlus
open System.Threading.Tasks
open Auth0
open Auth0.ManagementApi
open Auth0.ManagementApi.Models

let token = "..."

let client = Auth0.ManagementApi.ManagementApiClient(token, "https://redacted.ca.auth0.com/")

let req = GetUsersRequest()

let result = client.Users.GetAllAsync(req).Result

System.AggregateException: One or more errors occurred. (The format of value 'Bearer
’ is invalid.)

The token is valid JWT tested on JWT.io generated from the Auth0 Management API page. It also works in the docs.

Any idea what’s going on here?

Thanks in advance!

Hi @wake-reliant-poise,

The error strongly suggests that the token string being passed to the ManagementApiClient contains unexpected whitespace characters—most commonly a newline or an extra space—either before or after the actual JWT. The Auth0.ManagementApi client, which uses HttpClient internally, is very strict about the format of the Authorization header.

You can try using the .Trim() method to remove the whitespace from the token.

Please let us know if that resolves the issue.

Have a good one,
Vlad

1 Like

Hi Vlad,

I could have sworn I’d cleaned up the file I was loading the token from, but as it turns out, you’re right. Trim() solved it.

Thanks for the pointer!

1 Like

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