Chilkat Online Tools

ERROR!

------------------- GenerateCode ----------------------
ImpliedContentType: application/x-www-form-urlencoded
---- begin chilkat script ----
// This example assumes the Chilkat API to have been previously unlocked.
// See {{-global_unlock:::Global Unlock Sample-}} for sample code.

new Http http;
ckbool success;

new HttpRequest req;

new JsonObject jsonParam1;
call req.AddParam("token",jsonParam1.Emit());

new JsonObject jsonParam2;
call req.AddParam("client_id",jsonParam2.Emit());

call req.AddHeader("Authorization","Bearer <access_token>");

new HttpResponse resp;
req.HttpVerb = "POST";
req.ContentType = "application/x-www-form-urlencoded";
success = http.PostUrlEncoded("https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect", req,resp);
if (success == ckfalse) {
println http.LastErrorText;
return;
}

new StringBuilder sbResponseBody;
ignore = resp.GetBodySb(sbResponseBody);

new JsonObject jResp;
call jResp.LoadSb(sbResponseBody);
jResp.EmitCompact = ckfalse;

println "Response Body:";
println jResp.Emit();

int respStatusCode = resp.StatusCode;
println "Response Status Code = ",respStatusCode;
if (respStatusCode >= 400) {
    println "Response Header:";
    println resp.Header;
    println "Failed.";
    return;
}

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// {
//   "active": true,
//   "scope": "write",
//   "realm": "/",
//   "client_id": "forgerockDemoPublicClient",
//   "user_id": "demo",
//   "token_type": "Bearer",
//   "exp": 1597324710,
//   "sub": "demo",
//   "iss": "http://openam.example.com:8080/openam/oauth2",
//   "auth_level": 0,
//   "authGrantId": "NqnPcBkWNYRwRfnYPDKCGvSWLoo",
//   "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-52703",
//   "expires_in": 3600
// }

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: {{.https://tools.chilkat.io/jsonParse|||Generate JSON Parsing Code.}}

#ifdef IS_C_CPP
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
#undef IS_C_CPP


ckbool active = jResp.BoolOf("active");
string scope = jResp.StringOf("scope");
string realm = jResp.StringOf("realm");
string client_id = jResp.StringOf("client_id");
string user_id = jResp.StringOf("user_id");
string token_type = jResp.StringOf("token_type");
int exp = jResp.IntOf("exp");
string v_sub = jResp.StringOf("sub");
string iss = jResp.StringOf("iss");
int auth_level = jResp.IntOf("auth_level");
string authGrantId = jResp.StringOf("authGrantId");
string auditTrackingId = jResp.StringOf("auditTrackingId");
int expires_in = jResp.IntOf("expires_in");



---- end chilkat script ----

TCL / ForgeRock Identity Cloud Collection / Step 5: Introspect the Access Token

Back to Collection Items

load ./chilkat.dll

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set http [new_CkHttp]

set req [new_CkHttpRequest]

set jsonParam1 [new_CkJsonObject]

CkHttpRequest_AddParam $req "token" [CkJsonObject_emit $jsonParam1]

set jsonParam2 [new_CkJsonObject]

CkHttpRequest_AddParam $req "client_id" [CkJsonObject_emit $jsonParam2]

CkHttpRequest_AddHeader $req "Authorization" "Bearer <access_token>"

set resp [new_CkHttpResponse]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
ERROR: Assignment type mismatch.  ExpressionType=HttpResponse, atgType=ckbool

if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkJsonObject $jsonParam1
    delete_CkJsonObject $jsonParam2
    delete_CkHttpResponse $resp
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttpResponse_header $resp]
    puts "Failed."
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkJsonObject $jsonParam1
    delete_CkJsonObject $jsonParam2
    delete_CkHttpResponse $resp
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# Sample JSON response:
# (Sample code for parsing the JSON response is shown below)

# {
#   "active": true,
#   "scope": "write",
#   "realm": "/",
#   "client_id": "forgerockDemoPublicClient",
#   "user_id": "demo",
#   "token_type": "Bearer",
#   "exp": 1597324710,
#   "sub": "demo",
#   "iss": "http://openam.example.com:8080/openam/oauth2",
#   "auth_level": 0,
#   "authGrantId": "NqnPcBkWNYRwRfnYPDKCGvSWLoo",
#   "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-52703",
#   "expires_in": 3600
# }

# Sample code for parsing the JSON response...
# Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

set active [CkJsonObject_BoolOf $jResp "active"]
set scope [CkJsonObject_stringOf $jResp "scope"]
set realm [CkJsonObject_stringOf $jResp "realm"]
set client_id [CkJsonObject_stringOf $jResp "client_id"]
set user_id [CkJsonObject_stringOf $jResp "user_id"]
set token_type [CkJsonObject_stringOf $jResp "token_type"]
set exp [CkJsonObject_IntOf $jResp "exp"]
set v_sub [CkJsonObject_stringOf $jResp "sub"]
set iss [CkJsonObject_stringOf $jResp "iss"]
set auth_level [CkJsonObject_IntOf $jResp "auth_level"]
set authGrantId [CkJsonObject_stringOf $jResp "authGrantId"]
set auditTrackingId [CkJsonObject_stringOf $jResp "auditTrackingId"]
set expires_in [CkJsonObject_IntOf $jResp "expires_in"]

delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkJsonObject $jsonParam1
delete_CkJsonObject $jsonParam2
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	--data-urlencode 'token={{access_token}}'
	--data-urlencode 'client_id={{postmanPublicClientId}}'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect

Postman Collection Item JSON

{
  "name": "Step 5: Introspect the Access Token",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "// Tests",
          "",
          "const jsonData = JSON.parse(responseBody);",
          "",
          "pm.test(\"Status code is 200\", () => {",
          "  pm.expect(pm.response.code).to.eql(200);",
          "});",
          "",
          "pm.test(\"Response contains correct `client_id`.\", function () {",
          "    pm.expect(jsonData.client_id).to.eql(pm.collectionVariables.get(\"postmanPublicClientId\"));",
          "});",
          "",
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "token",
          "value": "{{access_token}}",
          "description": "Access token you want to introspect.",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{postmanPublicClientId}}",
          "description": "The ID of the public OAuth Client.",
          "type": "text"
        }
      ]
    },
    "url": {
      "raw": "{{amUrl}}/oauth2{{realm}}/introspect",
      "host": [
        "{{amUrl}}"
      ],
      "path": [
        "oauth2{{realm}}",
        "introspect"
      ]
    },
    "description": "Retrieve metadata about the active access token, such as, approved scopes, the user that authorized the token, and the expiry time."
  },
  "response": [
    {
      "name": "Example",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "urlencoded",
          "urlencoded": [
            {
              "key": "token",
              "value": "{{access_token}}",
              "description": "Access token you want to introspect.",
              "type": "text"
            },
            {
              "key": "client_id",
              "value": "{{postmanPublicClientId}}",
              "description": "The ID of the public OAuth Client.",
              "type": "text"
            }
          ]
        },
        "url": {
          "raw": "{{amUrl}}/oauth2{{realm}}/introspect",
          "host": [
            "{{amUrl}}"
          ],
          "path": [
            "oauth2{{realm}}",
            "introspect"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "X-Frame-Options",
          "value": "SAMEORIGIN"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "Content-Type",
          "value": "application/json;charset=UTF-8"
        },
        {
          "key": "Content-Length",
          "value": "345"
        },
        {
          "key": "Date",
          "value": "Thu, 13 Aug 2020 12:18:39 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"active\": true,\n    \"scope\": \"write\",\n    \"realm\": \"/\",\n    \"client_id\": \"forgerockDemoPublicClient\",\n    \"user_id\": \"demo\",\n    \"token_type\": \"Bearer\",\n    \"exp\": 1597324710,\n    \"sub\": \"demo\",\n    \"iss\": \"http://openam.example.com:8080/openam/oauth2\",\n    \"auth_level\": 0,\n    \"authGrantId\": \"NqnPcBkWNYRwRfnYPDKCGvSWLoo\",\n    \"auditTrackingId\": \"037f02f9-d821-4f72-8563-c5050c40fdc3-52703\",\n    \"expires_in\": 3600\n}"
    }
  ]
}