Back to Collection Items
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
#include <CkStringBuilderW.h>
void ChilkatSample(void)
{
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
bool success;
CkHttpRequestW req;
CkJsonObjectW jsonParam1;
req.AddParam(L"token",jsonParam1.emit());
CkJsonObjectW jsonParam2;
req.AddParam(L"client_id",jsonParam2.emit());
CkJsonObjectW jsonParam3;
req.AddParam(L"client_secret",jsonParam3.emit());
req.AddHeader(L"Authorization",L"Bearer <access_token>");
CkHttpResponseW *resp = http.PostUrlEncoded(L"https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect",req);
if (http.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
CkStringBuilderW sbResponseBody;
resp->GetBodySb(sbResponseBody);
CkJsonObjectW jResp;
jResp.LoadSb(sbResponseBody);
jResp.put_EmitCompact(false);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",jResp.emit());
int respStatusCode = resp->get_StatusCode();
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",resp->header());
wprintf(L"Failed.\n");
delete resp;
return;
}
delete resp;
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "active": true,
// "scope": "share",
// "realm": "/",
// "client_id": "forgerockDemoConfidentialClient",
// "user_id": "demo",
// "token_type": "Bearer",
// "exp": 1597324460,
// "sub": "demo",
// "iss": "http://openam.example.com:8080/openam/oauth2",
// "auth_level": 0,
// "authGrantId": "HZdG4E4NnNcP6k4T2bCTxPPeysg",
// "auditTrackingId": "037f02f9-d821-4f72-8563-c5050c40fdc3-50979",
// "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
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
bool active = jResp.BoolOf(L"active");
const wchar_t *scope = jResp.stringOf(L"scope");
const wchar_t *realm = jResp.stringOf(L"realm");
const wchar_t *client_id = jResp.stringOf(L"client_id");
const wchar_t *user_id = jResp.stringOf(L"user_id");
const wchar_t *token_type = jResp.stringOf(L"token_type");
int exp = jResp.IntOf(L"exp");
const wchar_t *v_sub = jResp.stringOf(L"sub");
const wchar_t *iss = jResp.stringOf(L"iss");
int auth_level = jResp.IntOf(L"auth_level");
const wchar_t *authGrantId = jResp.stringOf(L"authGrantId");
const wchar_t *auditTrackingId = jResp.stringOf(L"auditTrackingId");
int expires_in = jResp.IntOf(L"expires_in");
}
Curl Command
curl -X POST
-H "Authorization: Bearer <access_token>"
--data-urlencode 'token={{access_token}}'
--data-urlencode 'client_id={{postmanConfidentialClientId}}'
--data-urlencode 'client_secret={{postmanClientSecret}}'
https://<tenant-name>.forgeblocks.com/am/oauth2/realms/root/realms/alpha/introspect
Postman Collection Item JSON
{
"name": "Step 4: 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(\"postmanConfidentialClientId\"));",
"});",
"",
"",
""
],
"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": "{{postmanConfidentialClientId}}",
"description": "The ID of the Confidential OAuth Client.",
"type": "text"
},
{
"key": "client_secret",
"value": "{{postmanClientSecret}}",
"description": "The ID of the Confidential 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": "{{postmanConfidentialClientId}}",
"description": "The ID of the Confidential OAuth Client.",
"type": "text"
},
{
"key": "client_secret",
"value": "{{postmanClientSecret}}",
"description": "The ID of the Confidential 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": "351"
},
{
"key": "Date",
"value": "Thu, 13 Aug 2020 12:14:35 GMT"
}
],
"cookie": [
],
"body": "{\n \"active\": true,\n \"scope\": \"share\",\n \"realm\": \"/\",\n \"client_id\": \"forgerockDemoConfidentialClient\",\n \"user_id\": \"demo\",\n \"token_type\": \"Bearer\",\n \"exp\": 1597324460,\n \"sub\": \"demo\",\n \"iss\": \"http://openam.example.com:8080/openam/oauth2\",\n \"auth_level\": 0,\n \"authGrantId\": \"HZdG4E4NnNcP6k4T2bCTxPPeysg\",\n \"auditTrackingId\": \"037f02f9-d821-4f72-8563-c5050c40fdc3-50979\",\n \"expires_in\": 3600\n}"
}
]
}