# oAuth 2.0 automate token generation in Postman

**URL:** <https://community.dynatrace.com/t/oauth-2-0-automate-token-generation-in-postman/48757>\
**Category:** Dynatrace Tips\
**Tags:** account-management, postman, tips-and-tricks, dynatrace-api\
**Created:** [November 6, 2024, 9:44am UTC](https://community.dynatrace.com/t/oauth-2-0-automate-token-generation-in-postman/48757 "2024-11-06T09:44:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MostafaHussein](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/mostafahussein/32/2785_2.png) [@MostafaHussein](https://community.dynatrace.com/u/MostafaHussein)\
**Post date:** [November 6, 2024, 9:44am UTC](https://community.dynatrace.com/t/oauth-2-0-automate-token-generation-in-postman/48757/1 "2024-11-06T09:44:23Z")

</div>

It’s simple and efficient trick to leverage Postman scripting features to get automate access tokens generation process to gain faster implementation than creating new one each 5 minutes as it’s a limitation in Account APIs.

1) follow this path to create oAuth client (_Account Homepage \> select environment \> Identity & access management \> oAuth clients_)

 ![MostafaHussein_0-1730878043951.png](https://global.discourse-cdn.com/dynatrace/original/2X/2/2af6bb45ada82092d243871a9da0d7834eb224ba.png)

2) select the following scope&nbsp; **account-uac-read** (take note with this we’ll use it later)

take in consideration that the user you’re using for creating this client must has the following:

- User has to be active
- User must hold the&nbsp; **account-user-management** &nbsp;permission

 ![MostafaHussein_1-1730878473528.png](https://global.discourse-cdn.com/dynatrace/original/3X/e/f/efaebb7a2a5b2be27f2eb3611260eba5eea253bf.png)

3) before the client is created, open txt and save the following items in addition to the _scope_ we’ve saved previously as the above figure and click _Finish_:  
- client id  
- client secret  
- Dynatrace account URN  
- account uuid: extract it from the URN (_we will need it later_)

 ![MostafaHussein_2-1730879016268.png](https://global.discourse-cdn.com/dynatrace/original/2X/b/b2266767357f86d0f9bb59e1be21e432adb8794a.png)

4) open PostMan and create new collection as the following figure

 ![MostafaHussein_3-1730879439653.png](https://global.discourse-cdn.com/dynatrace/original/3X/8/1/812e61e7c4ff481d9eb76e92cd741efe3916670f.png)

5) open the collection, then go to Variables tab then create new variable named `oauth_token` for now leave it blank we’ll automate filling it in the next step

 ![MostafaHussein_5-1730879936076.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/91270d76b3c835bc488026c96a74fe6dbb9d3876.png)

6) create new request with, set the url to&nbsp;[https://sso.dynatrace.com/sso/oauth2/token](https://sso.dynatrace.com/sso/oauth2/token)&nbsp;and the method to POST, in headers tab set _ **Content-Type** _ to&nbsp;_ **application/x-www-form-urlencoded** _, finally rename it to ` **oAuth Token Generation** `

 ![MostafaHussein_6-1730880250008.png](https://global.discourse-cdn.com/dynatrace/original/3X/4/0/400a7e29c3333fc8c4f69b66d42f3e260f64b97d.png)

7) under scripts tab, use this code that extract the access\_token value from the response once the request is sent then set the variable `oauth_token` we made in the beginning with the captured value from the response

```javascript
pm.test("Set collectionVariables variable", () => {
    pm.collectionVariables.set("oauth_token", pm.response.json()['access_token']);
    console.log(pm.response.json());
});

```

 ![MostafaHussein_7-1730880607607.png](https://global.discourse-cdn.com/dynatrace/original/3X/a/5/a57c3647a26355cd246953cc467e9d2431be8f02.png)

😎 instead of adding all required parameters to the URL it’s little bit confusing, i’ll add it to the body of the request as the following figure

 ![MostafaHussein_8-1730880782974.png](https://global.discourse-cdn.com/dynatrace/original/3X/1/0/1095710cb1775ab5e6117b8f9749e55ec76aee0b.png)

9) save the collection and the oAuth request (very important), and try send the request “OAuth Token Generation” then go to the collection variables and see it’s value to validate it’s updated successfully.

 ![MostafaHussein_9-1730882012352.png](https://global.discourse-cdn.com/dynatrace/original/2X/2/20cb2d68393c8968ebc7c7593018612d4f6028f8.png)

 ![MostafaHussein_10-1730882160620.png](https://global.discourse-cdn.com/dynatrace/original/2X/f/ffb20da7a328b5e2940cab4cd266ec0f114acef1.png)

in the collection variables

 ![MostafaHussein_11-1730882219174.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/f/6f89edbd036c5dd32addfaf3cb964bc9923b7eae.png)

10) add new GET request and name it `subscriptions` with this endpoint&nbsp;[https://api.dynatrace.com/sub/v2/accounts/{accountUuid}/subscriptions](https://api.dynatrace.com/sub/v2/accounts/%7BaccountUuid%7D/subscriptions)&nbsp;and replace the {_accountUuid}_ with the value that has been captured in _step no. 3_, then add to headers the key **Authorization** &nbsp;and value ` **Bearer {{oauth\_token}}** ` as the _oauth\_token_ is the variable name that has been set when executed the request named “ **OAuth Token Generation** ”, see this reference link&nbsp;[Dynatrace Platform Subscription API - GET all subscriptions — Dynatrace Docs](https://docs.dynatrace.com/docs/shortlink/account-api-dps-subscriptions-get-all)

 ![MostafaHussein_0-1730885644523.png](https://global.discourse-cdn.com/dynatrace/original/3X/b/2/b2d4157daa6cdff0b079af6ae0361dd8d90ec91a.png)

BR,  
Mostafa Hussein.

---

<div class="post-metadata">

**Author:** ![p\_devulapalli](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/p_devulapalli/32/41590_2.png) [@p\_devulapalli](https://community.dynatrace.com/u/p_devulapalli)\
**Post date:** [November 8, 2024, 12:15am UTC](https://community.dynatrace.com/t/oauth-2-0-automate-token-generation-in-postman/48757/2 "2024-11-08T00:15:24Z")

</div>

[@MostafaHussein](https://community.dynatrace.com/user/66935)&nbsp;- Good one !!
