Skip to content

Instantly share code, notes, and snippets.

@mamund
Last active November 27, 2023 14:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamund/ead9a7d728a60a69ec7555767d01dac2 to your computer and use it in GitHub Desktop.
Save mamund/ead9a7d728a60a69ec7555767d01dac2 to your computer and use it in GitHub Desktop.
auth0 JWT in Postman API Testing
/************************************************
Retrieve a JWT from Auth0 for Postman
2020-05-15 : @mamund @greatwebapis
NOTES:
1) create API definition in Auth0 with
"client_credential" (machine-to-machine)
2) pull the following from Auth0 API config:
- domain
- client_id
- client_secret
- audience
3) set environment vars in Postman with
the Auth0 values
4) add the code below to your collection's
Pre-request Scripts page in Postman.
5) in any request that needs a value JWT
- add "authorization" header with an
{{authorization}} value placeholder
- call this function any time you
need a new JWT.
*************************************************/
function setAuth0JWT() {
// fill local vars from Postman environment
var domain = pm.environment.get("domain");
var client_id = pm.environment.get("client_id");
var client_secret = pm.environment.get("client_secret");
var audience = pm.environment.get("audience");
// load request object
var req = {
url: "https://" + domain + "/oauth/token",
method: "POST",
header: {
"accept": "application/json",
"content-type": "application/json"
},
body: {
mode: 'raw',
raw: JSON.stringify(
{
client_id: client_id,
client_secret: client_secret,
audience: audience,
grant_type: "client_credentials"
}
)
}
};
// execute request for JWT, store token, set header
pm.sendRequest(req, function (err, res) {
pm.environment.set("authorization", "Bearer " + res.json().access_token);
pm.request.headers.add(
{
key: "authorization",
value: pm.environment.get("authorization")
}
);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment