# How to Get Bearer Token programatically?

**URL:** <https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659>\
**Category:** Get Help\
**Created:** [July 1, 2020, 9:43pm UTC](https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659 "2020-07-01T21:43:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafae11](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rafae11/32/22319_2.png) [@rafae11](https://community.auth0.com/u/rafae11)\
**Post date:** [July 1, 2020, 9:43pm UTC](https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659/1 "2020-07-01T21:43:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![preethi.ramiah](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/preethi.ramiah/32/13390_2.png) [@preethi.ramiah](https://community.auth0.com/u/preethi.ramiah)\
**Post date:** [July 2, 2020, 5:48am UTC](https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659/2 "2020-07-02T05:48:04Z")

</div>

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
  }

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [July 2, 2020, 10:28am UTC](https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659/3 "2020-07-02T10:28:54Z")

</div>

Thanks for sharing that with the rest of community!

---

<div class="post-metadata">

**Author:** ![rafae11](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rafae11/32/22319_2.png) [@rafae11](https://community.auth0.com/u/rafae11)\
**Post date:** [July 2, 2020, 4:03pm UTC](https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659/4 "2020-07-02T16:03:43Z")

</div>

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']) 

});

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 17, 2020, 4:03pm UTC](https://community.auth0.com/t/how-to-get-bearer-token-programatically/45659/5 "2020-07-17T16:03:44Z")

</div>

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