# OAuth2 Token into Body

**URL:** <https://community.postman.com/t/oauth2-token-into-body/11636>\
**Category:** Help Hub\
**Created:** [March 31, 2020, 9:07pm UTC](https://community.postman.com/t/oauth2-token-into-body/11636 "2020-03-31T21:07:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grehkemper](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/grehkemper/32/4096_2.png) [@grehkemper](https://community.postman.com/u/grehkemper)\
**Post date:** [March 31, 2020, 9:07pm UTC](https://community.postman.com/t/oauth2-token-into-body/11636/1 "2020-03-31T21:07:16Z")

</div>

Hi, I am using the “Oauth 2” - “Get New Access Token” functionality, where the token will be auto-filled into the “Access Token” field and then it allows me to “Add authorization data to Request URL or Request Headers”

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/c/8cbedb5eeb41e22b79e27936d0fd262eab6564ff.png)

My problem is: My API needs me to pass the token as an entry in the BODY (specifically, a key called `token` as `x-www-form-urlencoded`) - is there any way I can get the token to be placed in the _body_?

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [April 1, 2020, 4:12am UTC](https://community.postman.com/t/oauth2-token-into-body/11636/2 "2020-04-01T04:12:31Z")

</div>

Hi @grehkemper!

Welcome to the community! 👏

In regards to your issue, what I would suggest in this instance is to obtain the authorization token via a pre-request script, and then saving that token as an environment variable, such as {{authToken}}.

Then using that in your form-urlencoded payload to your service, by just adding the key value pair (token / {{authToken}}.

Not sure how you need to format your request to obtain your token, but if it follows the OAuth 2.0 spec, something like so should work:

```
pm.sendRequest({
  url: pm.variables.get("Auth_Url"), 
  method: 'POST',
  header: {
    'Accept': 'application/json',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': pm.variables.get("Basic_Auth")
  },
  body: {
      mode: 'urlencoded',
      urlencoded: [
        {key: "grant_type", value: "password", disabled: false},
        {key: "username", value: pm.environment.get("OAUTH_USERNAME"), disabled: false},
        {key: "password", value: pm.environment.get("OAUTH_PASSWORD"), disabled: false}
    ]
  }
}
  }, function (err, res) {
    pm.environment.set("authToken", res.json().access_token);
    pm.environment.set("OAuth_Timestamp", new Date());
    
    // Set the ExpiresInTime variable to the time given in the response if it exists
    if(res.json().expires_in){
        expiresInTime = res.json().expires_in * 1000;
    }
    pm.environment.set("ExpiresInTime", expiresInTime);
  });

```

Filling in the correct key value pairs above

\*Obtained from: [How to Automate OAuth2 Token Renewal in Postman | by Allen Helton | Medium](https://medium.com/@allen.helton/how-to-automate-oauth2-token-renewal-in-postman-864420d381a0)

And

[https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990](https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990)

Hope this helps!

Orest

---

<div class="post-metadata">

**Author:** ![grehkemper](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/grehkemper/32/4096_2.png) [@grehkemper](https://community.postman.com/u/grehkemper)\
**Post date:** [April 3, 2020, 6:13pm UTC](https://community.postman.com/t/oauth2-token-into-body/11636/3 "2020-04-03T18:13:12Z")

</div>

@odanylewycz thanks a lot for the response. I did find a solution based on the example and some of the links your provided. It’s a super-simple one-line pre-request script:

```
pm.variables.set('mytoken', pm.request.auth.oauth2.get('accessToken'));

```

… and then i use use `{{mytoken}}` in the body where i need it.

Thanks!

---

<div class="post-metadata">

**Author:** ![odanylewycz](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/odanylewycz/32/3240_2.png) [@odanylewycz](https://community.postman.com/u/odanylewycz)\
**Post date:** [April 3, 2020, 8:17pm UTC](https://community.postman.com/t/oauth2-token-into-body/11636/4 "2020-04-03T20:17:01Z")

</div>

Hi @grehkemper!

Wow thats sweet! I actually had no clue those methods were available through the `pm` class in postman’s node js module!

I learned something new. Thanks for teaching me 🙂.

Regards,  
Orest

---

<div class="post-metadata">

**Author:** ![aurelien\_boubennec](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@aurelien\_boubennec](https://community.postman.com/u/aurelien_boubennec)\
**Post date:** [March 10, 2022, 4:52pm UTC](https://community.postman.com/t/oauth2-token-into-body/11636/5 "2022-03-10T16:52:05Z")

</div>

Thank you that was super-helpful
