forked from alexatiks/keycloak-postman-pre-request
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycloak-fetch-token-postman-pre-request.js
31 lines (29 loc) · 1.33 KB
/
keycloak-fetch-token-postman-pre-request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var server = ""; // add your Keycloak-URL here (without /auth)
var realm = ""; // the name of the realm
var grantType = "password"; // the granttype, with password you can login as a normal user
var clientId = ""; // the name of the client you created in Keycloak
var clientSecret = ""; // the secret you copied earlier
var username = ""; // the username of the user you want to test with
var password = ""; // the password of the user you want to test with
// creating the request URL
var url = `${server}/auth/realms/${realm}/protocol/openid-connect/token`;
// creating the body of the request
var data = `grant_type=${grantType}&client_id=${clientId}&username=${username}&password=${password}&client_secret=${clientSecret}`;
// request to Keycloak
// read more about this here: https://www.keycloak.org/docs/latest/authorization_services/#_service_overview
pm.sendRequest({
url: url,
method: 'POST',
header: { 'Content-Type': 'application/x-www-form-urlencoded'},
body: {
mode: 'raw',
raw: data
}
}, function(err, response) {
// Set the environment variable for token
var response_json = response.json();
var token = response_json.access_token;
pm.environment.set('token', token);
// You can open up the console in Postman with Alt + Ctrl + C
console.log(token);
});