Auth0 Home Blog Docs

401 Calling /token Only In JS



I’m trying to retrieve an auth token so I can later get user info, using this guide:

However, I get a 401 Unauthorized when calling from my React JS client.

I had this working exactly the same code in a previous vanilla JS client, and the equivalent CURL returns a response with a token so I can’t see what’s causing this issue.

I’m posting:
{ "grant_type": "client_credentials", "client_id": xxx, "client_secret": xxx, "audience": "" };

to ‘

and as I say, CURL works:

curl --request POST --url '' --header 'content-type: application/json' --data '{"grant_type":"client_credentials","client_id": "xxx","client_secret": "xxx","audience": ""}'

so the credentials definitely match the machine-to-machine application but the JS call does not. There’s nothing useful in the returned error except “401 unauthorised”


Are you using the same client on both calls?

By definition a React JS application is a public client (token endpoint authentication method set to None) and can’t execute the client-credentials exchange (which is reserve for confidential clients only).

The Management API v2 is not designed to be accessed by SPAs applications, unless done on behalf of the user (for very limited scopes like read:current_user). For these cases you would request an access token using the user interactive token request /authorize.

In general, though, if you are trying to get user information the recommendation would be to use the OIDC /userinfo endpoint, with the access token obtained through an authorization request with scope=openid.

closed #3

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