How to Get Bearer Token programatically?

I need to automate some tests in postman, at the moment my solution is manual.
I login to the site i’m testing and check the developer tools in chrome and get the bearer token.
and copy it to the environment variable.

Is there a way to extract the bearer token programatically?

We do the following to get bearer token: client is created using createAuth0Client()

client.getIdToken = async function () {
    await client.getTokenSilently()
    const { cache } = client.cache
    return cache[Object.keys(cache)[0]].id_token
  }
1 Like

Thanks for sharing that with the rest of community!

I managed to get an access token by using the script below, i’m still having an issue. The requests
that uses the global variable jwttoken. fails to authenticate. i get Bearer error=“invalid_token”,
error_description=“The issuer is invalid”

var options = {
 
method: 'POST',

  url: pm.variables.get('auth0_url'),

  header: {

    "Content-Type": "application/json"

  },

  body: {

    mode: 'raw',

    raw: JSON.stringify({

      client_id: pm.variables.get('client_id'),

      client_secret: pm.variables.get('client_secret'),

      audience: pm.variables.get('web_url'),

      grant_type: 'client_credentials'

    })

  }

}

pm.sendRequest(options, function (error, response, body) {

  if (error) throw new Error(error);

  var json = JSON.parse(response.text());

  pm.globals.set("jwttoken",json['access_token']) 

});

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