-
-
Save P0SlX/6ffea0a065a4587e1e9586bcd8e33cac to your computer and use it in GitHub Desktop.
Postman pre-request script to automatically get a bearer token from Auth0 and save it for reuse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let getToken = true; | |
| if (pm.collectionVariables.get('accessTokenExpiry') > (new Date()).getTime()){ | |
| getToken = false; | |
| } | |
| if (getToken) { | |
| const echoPostRequest = { | |
| url: pm.collectionVariables.get('accessTokenURL'), | |
| method: 'POST', | |
| body: { | |
| mode: 'formdata', | |
| formdata: [ | |
| {key: "client_id", value: pm.collectionVariables.get('clientID')}, | |
| {key: "client_secret", value: pm.collectionVariables.get('clientSecret')}, | |
| {key: "scope", value: pm.collectionVariables.get('scope')}, | |
| {key: "grant_type", value:"client_credentials"} | |
| ] | |
| } | |
| }; | |
| pm.sendRequest(echoPostRequest, function (err, res) { | |
| if (!err) { | |
| const responseJson = res.json(); | |
| pm.collectionVariables.set('currentAccessToken', responseJson.access_token) | |
| const expiryDate = new Date(); | |
| expiryDate.setSeconds(expiryDate.getSeconds() + responseJson.expires_in); | |
| pm.collectionVariables.set('accessTokenExpiry', expiryDate.getTime()); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment