Hello,
I have a React application and a .NET Core Web API, both registered and secured with Auth0.
I have had no issues getting the React app wired up to Auth0, and can log my test users in without issue.
When trying to get an auth token for the API however, I keep getting an error:
{error: "access_denied", error_description: "Unauthorized"}
The POST request is based on the code provided in the Auth0 dashboard, under the “test” tab for my API.
My steps are:
- Log in user to React app
- Execute an axios call to request an auth token for the API
- Attach this token as an Authentication header for API requests.
- Winning!
However, I’m currently not winning, and being blocked at step 2. Here’s the code that makes the request for the token.
I am a JS/React rookie so, it’s likely I’ve made a mistake:
export async function getApiAuthToken() {
var result;
await axios({
method: "post",
url: "https://aimanager.us.auth0.com/oauth/token",
headers: { "content-type": "application/json" },
body:
'{"client_id":"<MY_CLIENT_ID","client_secret":"<MY_SECRET>","audience":"<MY_AUDIENCE>","grant_type":"client_credentials"}',
})
.then((response) => (result = response))
.catch(handleError);
return result;
}