Back to Collection Items
integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Req
oleobject loo_JsonParam3
oleobject loo_JsonParam4
oleobject loo_JsonParam6
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Client_id
string ls_Code
string ls_Iss
string ls_Data
string ls_Accept
string ls_Accept_Encoding
string ls_Cookie
string ls_Host
string ls_Referer
string ls_User_Agent
string ls_X_Amzn_Trace_Id
string ls_Json
string ls_Method
string ls_Origin
string ls_Url
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Http = create oleobject
// Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Req = create oleobject
// Use "Chilkat_9_5_0.HttpRequest" for versions of Chilkat < 10.0.0
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")
loo_Req.AddParam("scope","write")
loo_Req.AddParam("response_type","code")
loo_JsonParam3 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam3.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("client_id",loo_JsonParam3.Emit())
loo_JsonParam4 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam4.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("redirect_uri",loo_JsonParam4.Emit())
loo_Req.AddParam("decision","allow")
loo_JsonParam6 = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JsonParam6.ConnectToNewObject("Chilkat.JsonObject")
loo_Req.AddParam("csrf",loo_JsonParam6.Emit())
loo_Req.AddParam("state","abc123")
loo_Req.AddParam("service","PasswordGrant")
loo_Req.AddHeader("Authorization","Bearer <access_token>")
loo_Req.AddHeader("Cookie","{{cookieName}}={{demoSSOToken}}")
loo_Resp = loo_Http.PostUrlEncoded("https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/authorize",loo_Req)
if loo_Http.LastMethodSuccess = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_Req
destroy loo_JsonParam3
destroy loo_JsonParam4
destroy loo_JsonParam6
return
end if
loo_SbResponseBody = create oleobject
// Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")
loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
// Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")
loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0
Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()
li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
Write-Debug "Response Header:"
Write-Debug loo_Resp.Header
Write-Debug "Failed."
destroy loo_Resp
destroy loo_Http
destroy loo_Req
destroy loo_JsonParam3
destroy loo_JsonParam4
destroy loo_JsonParam6
destroy loo_SbResponseBody
destroy loo_JResp
return
end if
destroy loo_Resp
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "args": {
// "client_id": "forgerockDemoConfidentialClient",
// "code": "oIQFFQ1S5Fxnqs5U_EMWxYEJktQ",
// "iss": "http://openam.example.com:8080/openam/oauth2"
// },
// "data": "",
// "files": {},
// "form": {},
// "headers": {
// "Accept": "*/*",
// "Accept-Encoding": "gzip, deflate, br",
// "Cookie": "amlbcookie=01; iPlanetDirectoryPro=sOpI1RvbCgvlQk.*AAJTSQACMDEA.*",
// "Host": "httpbin.org",
// "Referer": "http://openam.example.com:8080/openam/oauth2/realms/root/authorize",
// "User-Agent": "PostmanRuntime/7.26.3",
// "X-Amzn-Trace-Id": "Root=1-5f352dc7-0d6bd5fceafcd01e856febd2"
// },
// "json": null,
// "method": "GET",
// "origin": "5.65.200.229",
// "url": "https://httpbin.org/anything?code=oIQFFQ1S5Fxnqs5U_EMWxYEJktQ&iss=http:%2F%2Fopenam.example.com:8080%2Fopenam%2Foauth2&client_id=forgerockDemoConfidentialClient"
// }
// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
ls_Client_id = loo_JResp.StringOf("args.client_id")
ls_Code = loo_JResp.StringOf("args.code")
ls_Iss = loo_JResp.StringOf("args.iss")
ls_Data = loo_JResp.StringOf("data")
ls_Accept = loo_JResp.StringOf("headers.Accept")
ls_Accept_Encoding = loo_JResp.StringOf("headers.Accept-Encoding")
ls_Cookie = loo_JResp.StringOf("headers.Cookie")
ls_Host = loo_JResp.StringOf("headers.Host")
ls_Referer = loo_JResp.StringOf("headers.Referer")
ls_User_Agent = loo_JResp.StringOf("headers.User-Agent")
ls_X_Amzn_Trace_Id = loo_JResp.StringOf("headers.X-Amzn-Trace-Id")
ls_Json = loo_JResp.StringOf("json")
ls_Method = loo_JResp.StringOf("method")
ls_Origin = loo_JResp.StringOf("origin")
ls_Url = loo_JResp.StringOf("url")
destroy loo_Http
destroy loo_Req
destroy loo_JsonParam3
destroy loo_JsonParam4
destroy loo_JsonParam6
destroy loo_SbResponseBody
destroy loo_JResp
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
-H "Cookie: {{cookieName}}={{demoSSOToken}}"
--data-urlencode 'scope=write'
--data-urlencode 'response_type=code'
--data-urlencode 'client_id={{postmanConfidentialClientId}}'
--data-urlencode 'redirect_uri={{redirect_uri}}'
--data-urlencode 'decision=allow'
--data-urlencode 'csrf={{demoSSOToken}}'
--data-urlencode 'state=abc123'
--data-urlencode 'service=PasswordGrant'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/authorize
Postman Collection Item JSON
{
"name": "Step 2: Get Authorization Code",
"event": [
{
"listen": "test",
"script": {
"exec": [
"const jsonData = pm.response.json();",
"",
"if(pm.response.code == 200)",
"{",
" if(jsonData.args.code && jsonData.args.code != \"\") {",
" pm.globals.set(\"authorization_code\", jsonData.args.code);",
" }",
"}",
"",
"// TESTS",
"",
"pm.test(\"Follow redirects is enabled in Postman (Status code not 302)\", () => {",
" // If response was 302, ensure Postman is following redirects. ",
" pm.response.to.not.have.status(302);",
"});",
"",
"pm.test(\"Response from httpbin contained `code` argument\", () => {",
" pm.expect(jsonData.args.code).to.be.a(\"string\");",
"});"
],
"type": "text/javascript"
}
}
],
"protocolProfileBehavior": {
"followRedirects": true
},
"request": {
"method": "POST",
"header": [
{
"key": "Cookie",
"value": "{{cookieName}}={{demoSSOToken}}",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "scope",
"value": "write ",
"description": "Strings that are presented to the user for approval and included in tokens so that the protected resource may make decisions about what to give access to.",
"type": "text"
},
{
"key": "response_type",
"value": "code",
"description": "Response types the client will support and use.",
"type": "text"
},
{
"key": "client_id",
"value": "{{postmanConfidentialClientId}}",
"description": "The ID of the Confidential OAuth Client.",
"type": "text"
},
{
"key": "redirect_uri",
"value": "{{redirect_uri}}",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "decision",
"value": "allow",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "csrf",
"value": "{{demoSSOToken}}",
"description": "SSO token of the demo user, to protect against cross-site request forgery.",
"type": "text"
},
{
"key": "state",
"value": "abc123",
"type": "text"
},
{
"key": "service",
"value": "PasswordGrant",
"type": "text"
}
]
},
"url": {
"raw": "{{amUrl}}/oauth2{{realm}}/authorize",
"host": [
"{{amUrl}}"
],
"path": [
"oauth2{{realm}}",
"authorize"
]
},
"description": "Get the authorization code by making a call to the authorization server's authorization endpoint, specifying the SSO token of the user.\n"
},
"response": [
{
"name": "Success - headers captured by HttpBin.",
"originalRequest": {
"method": "POST",
"header": [
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "scope",
"value": "write ",
"description": "Strings that are presented to the user for approval and included in tokens so that the protected resource may make decisions about what to give access to.",
"type": "text"
},
{
"key": "response_type",
"value": "code",
"description": "Response types the client will support and use.",
"type": "text"
},
{
"key": "client_id",
"value": "{{postmanConfidentialClientId}}",
"description": "The ID of the Confidential OAuth Client.",
"type": "text"
},
{
"key": "redirect_uri",
"value": "{{redirect_uri}}",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "decision",
"value": "allow",
"description": "The complete URI to which client redirects the user if the request is successful.",
"type": "text"
},
{
"key": "csrf",
"value": "{{demoSSOToken}}",
"description": "SSO token of the demo user, to protect against cross-site request forgery.",
"type": "text"
}
]
},
"url": {
"raw": "{{amUrl}}/oauth2{{realm}}/authorize",
"host": [
"{{amUrl}}"
],
"path": [
"oauth2{{realm}}",
"authorize"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Thu, 13 Aug 2020 12:10:47 GMT"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Content-Length",
"value": "937"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Server",
"value": "gunicorn/19.9.0"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Allow-Credentials",
"value": "true"
}
],
"cookie": [
],
"body": "{\n \"args\": {\n \"client_id\": \"forgerockDemoConfidentialClient\",\n \"code\": \"oIQFFQ1S5Fxnqs5U_EMWxYEJktQ\",\n \"iss\": \"http://openam.example.com:8080/openam/oauth2\"\n },\n \"data\": \"\",\n \"files\": {},\n \"form\": {},\n \"headers\": {\n \"Accept\": \"*/*\",\n \"Accept-Encoding\": \"gzip, deflate, br\",\n \"Cookie\": \"amlbcookie=01; iPlanetDirectoryPro=sOpI1RvbCgvlQk.*AAJTSQACMDEA.*\",\n \"Host\": \"httpbin.org\",\n \"Referer\": \"http://openam.example.com:8080/openam/oauth2/realms/root/authorize\",\n \"User-Agent\": \"PostmanRuntime/7.26.3\",\n \"X-Amzn-Trace-Id\": \"Root=1-5f352dc7-0d6bd5fceafcd01e856febd2\"\n },\n \"json\": null,\n \"method\": \"GET\",\n \"origin\": \"5.65.200.229\",\n \"url\": \"https://httpbin.org/anything?code=oIQFFQ1S5Fxnqs5U_EMWxYEJktQ&iss=http:%2F%2Fopenam.example.com:8080%2Fopenam%2Foauth2&client_id=forgerockDemoConfidentialClient\"\n}"
}
]
}