cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

oAuth 2.0 automate token generation in Postman

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

2) select the following scope 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 account-user-management permission

MostafaHussein_1-1730878473528.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

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

MostafaHussein_3-1730879439653.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

 

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

MostafaHussein_6-1730880250008.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

 

 

 

 

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

 

 

 

 

MostafaHussein_7-1730880607607.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

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

MostafaHussein_10-1730882160620.png

in the collection variables

MostafaHussein_11-1730882219174.png

10) add new GET request and name it `subscriptions` with this endpoint https://api.dynatrace.com/sub/v2/accounts/{accountUuid}/subscriptions and replace the {accountUuid} with the value that has been captured in step no. 3, then add to headers the key Authorization 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 https://docs.dynatrace.com/docs/shortlink/account-api-dps-subscriptions-get-all 

MostafaHussein_0-1730885644523.png

BR,
Mostafa Hussein.

 

Certified Dynatrace Professional | Certified Dynatrace Services - Observability | Dynatrace Partner yourcompass.ca
1 REPLY 1

p_devulapalli
Champion

@MostafaHussein - Good one !!

Phani Devulapalli

Featured Posts