# Automatically obtain OAuth 2.0 Token

**URL:** <https://community.postman.com/t/automatically-obtain-oauth-2-0-token/46330>\
**Category:** Help Hub\
**Tags:** variables, authentication, oauth2\
**Created:** [April 13, 2023, 4:49pm UTC](https://community.postman.com/t/automatically-obtain-oauth-2-0-token/46330 "2023-04-13T16:49:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vandanak49](https://avatars.discourse-cdn.com/v4/letter/v/dbc845/32.png) [@vandanak49](https://community.postman.com/u/vandanak49)\
**Post date:** [April 13, 2023, 4:49pm UTC](https://community.postman.com/t/automatically-obtain-oauth-2-0-token/46330/1 "2023-04-13T16:49:29Z")

</div>

Hi All,

I am trying to get an OAuth 2.0 access token by authorizing using browser. However, it needs manual intervention to click “Get New Access Token.” Is there a way to automate this in the pre-requisite script? I have this snippet, but now sure how to mention “Authorize using browser” and “Get New Access Token”.

```auto
const tokenUrl = ' ';
const redirectUrl = ' ';

const clientId = ' ';
const clientSecret = ' ';
const scope = ' ';
const audience = ' '

const getTokenRequest = {
  method: 'POST',
  url: tokenUrl,
  body: {
      mode: 'formdata',
      formdata: [
          { key: 'grant_type', value: 'authorization_code' },
          { key: 'redirect_uri', value: redirectUrl},
          { key: 'client_id', value: clientId },
          { key: 'client_secret', value: clientSecret },
          { key: 'scope', value: scope },
          { key: 'state', value: 'a_state' },
          { key: 'audience', value: audience }
      ]
  }
};

```

pm.sendRequest(getTokenRequest, (err, response) =\> {  
const jsonResponse = response.json();  
const newAccessToken = jsonResponse.access\_token;

pm.variables.set(‘access\_token’, newAccessToken);  
});

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 14, 2023, 10:38am UTC](https://community.postman.com/t/automatically-obtain-oauth-2-0-token/46330/2 "2023-04-14T10:38:06Z")

</div>

Same question as here.

[Pre-request script to automate authorization code grant type - Help - Postman](https://community.postman.com/t/pre-request-script-to-automate-authorization-code-grant-type/45989)
