06 Nov 2024 09:44 AM - last edited on 12 Nov 2024 01:12 PM by MaciejNeumann
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)
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:
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)
4) open PostMan and create new collection as the following figure
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
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`
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());
});
😎 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
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.
in the collection variables
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
BR,
Mostafa Hussein.
08 Nov 2024 12:15 AM
@MostafaHussein - Good one !!