Unable to get access token via auth token API

The auth0 API doesn’t work and I need help.

Postman script:

const auth0Domain = pm.environment.get('AUTH0_DOMAIN');
const clientId = pm.environment.get('AUTH0_CLIENT_ID');
const clientSecret = pm.environment.get('AUTH0_CLIENT_SECRET');
const audience = pm.environment.get('AUTH0_AUDIENCE');

const tokenUrl = `https://${auth0Domain}/oauth/token`;
const username = pm.environment.get('USER_EMAIL');
const password = pm.environment.get('USER_PASSWORD');

console.log(
    {
        auth0Domain, clientId, clientSecret, audience, tokenUrl, username, password
    }
)

const getTokenRequest = {
  method: 'POST',
  url: tokenUrl,
  header: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: {
    mode: 'urlencoded',
    urlencoded: [
      {key: 'grant_type', value: 'password'},
      {key: 'client_id', value: clientId},
      {key: 'client_secret', value: clientSecret},
      {key: 'username', value: username},
      {key: 'password', value: password},
      {key: 'audience', value: audience},
      {key: 'scope', value: 'openid profile email'}
    ]
  }
};

pm.sendRequest(getTokenRequest, (err, response) => {
  if (err) {
    console.error(err);
  } else {
    const jsonResponse = response.json();
    const newAccessToken = jsonResponse.access_token;
    pm.environment.set('ACCESS_TOKEN', newAccessToken);
    console.log('New access token set:', newAccessToken);
  }
});

error

{"error":"invalid_request","error_description":"invalid audience specified for password grant exchange"}

Everything in auth0 are configured but the API doesn’t work. the tenant is configured exact same as different tenant where the script works. what is the thing could be wrong?

Why are using even using password grant type?

Hi @yohamta,

Welcome to the Auth0 Community!

Can you check the audience and see if there is a trailing slash? Another Community member had a similar issue here: Password grant for API via Android Lock - #3 by Yann

Thanks,

Mary Beth

Thanks a lot for your help. I have just delete and re-created API and Application and now everything works fine. BR,

1 Like

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