unicodeC / Salesforce Platform APIs / OpenID Connect Dynamic Client Registration Endpoint
Back to Collection Items
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
HCkHttpW http;
BOOL success;
HCkJsonObjectW json;
HCkHttpResponseW resp;
HCkStringBuilderW sbResponseBody;
HCkJsonObjectW jResp;
int respStatusCode;
const wchar_t *strVal;
const wchar_t *client_id;
const wchar_t *client_secret;
const wchar_t *registration_access_token;
const wchar_t *registration_client_uri;
int client_id_issued_at;
int client_secret_expires_at;
const wchar_t *token_endpoint_auth_method;
const wchar_t *client_name;
int i;
int count_i;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
// Use this online tool to generate code from sample JSON: Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "redirect_uris": [
// "http://localhost"
// ],
// "response_types": [
// "code",
// "token",
// "id_token"
// ],
// "grant_types": [
// "authorization_code",
// "implicit",
// "refresh_token"
// ],
// "application_type": "web",
// "contacts": [
// "abc@sf.com",
// "ve7jtb@example.org"
// ],
// "client_name": "Example Olivier"
// }
json = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(json,L"redirect_uris[0]",L"http://localhost");
CkJsonObjectW_UpdateString(json,L"response_types[0]",L"code");
CkJsonObjectW_UpdateString(json,L"response_types[1]",L"token");
CkJsonObjectW_UpdateString(json,L"response_types[2]",L"id_token");
CkJsonObjectW_UpdateString(json,L"grant_types[0]",L"authorization_code");
CkJsonObjectW_UpdateString(json,L"grant_types[1]",L"implicit");
CkJsonObjectW_UpdateString(json,L"grant_types[2]",L"refresh_token");
CkJsonObjectW_UpdateString(json,L"application_type",L"web");
CkJsonObjectW_UpdateString(json,L"contacts[0]",L"abc@sf.com");
CkJsonObjectW_UpdateString(json,L"contacts[1]",L"ve7jtb@example.org");
CkJsonObjectW_UpdateString(json,L"client_name",L"Example Olivier");
CkHttpW_SetRequestHeader(http,L"Content-Type",L"application/json");
// Adds the "Authorization: Bearer {{init_access_token}}" header.
CkHttpW_putAuthToken(http,L"{{init_access_token}}");
CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");
resp = CkHttpW_PostJson3(http,L"https://login.salesforce.com{{site}}/services/oauth2/register",L"application/json",json);
if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
return;
}
sbResponseBody = CkStringBuilderW_Create();
CkHttpResponseW_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObjectW_Create();
CkJsonObjectW_LoadSb(jResp,sbResponseBody);
CkJsonObjectW_putEmitCompact(jResp,FALSE);
wprintf(L"Response Body:\n");
wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
respStatusCode = CkHttpResponseW_getStatusCode(resp);
wprintf(L"Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
wprintf(L"Response Header:\n");
wprintf(L"%s\n",CkHttpResponseW_header(resp));
wprintf(L"Failed.\n");
CkHttpResponseW_Dispose(resp);
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
return;
}
CkHttpResponseW_Dispose(resp);
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "client_id": "<client_id>",
// "client_secret": "<client_secret>",
// "registration_access_token": "<token>",
// "registration_client_uri": "https://login.salesforce.com/services/oauth2/register/<client_id>",
// "client_id_issued_at": <timestamp>,
// "client_secret_expires_at": 0,
// "token_endpoint_auth_method": "client_secret_post",
// "redirect_uris": [
// "http://localhost"
// ],
// "response_types": [
// "code",
// "token",
// "id_token"
// ],
// "grant_types": [
// "authorization_code",
// "hybrid_auth_code",
// "implicit",
// "refresh_token"
// ],
// "contacts": [
// "abc@sf.com"
// ],
// "client_name": "Sample Customer",
// "scopes": [
// "openid",
// "refresh_token",
// "id",
// "api"
// ]
// }
// 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.
client_id = CkJsonObjectW_stringOf(jResp,L"client_id");
client_secret = CkJsonObjectW_stringOf(jResp,L"client_secret");
registration_access_token = CkJsonObjectW_stringOf(jResp,L"registration_access_token");
registration_client_uri = CkJsonObjectW_stringOf(jResp,L"registration_client_uri");
client_id_issued_at = CkJsonObjectW_IntOf(jResp,L"client_id_issued_at");
client_secret_expires_at = CkJsonObjectW_IntOf(jResp,L"client_secret_expires_at");
token_endpoint_auth_method = CkJsonObjectW_stringOf(jResp,L"token_endpoint_auth_method");
client_name = CkJsonObjectW_stringOf(jResp,L"client_name");
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"redirect_uris");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"redirect_uris[i]");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"response_types");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"response_types[i]");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"grant_types");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"grant_types[i]");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"contacts");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"contacts[i]");
i = i + 1;
}
i = 0;
count_i = CkJsonObjectW_SizeOfArray(jResp,L"scopes");
while (i < count_i) {
CkJsonObjectW_putI(jResp,i);
strVal = CkJsonObjectW_stringOf(jResp,L"scopes[i]");
i = i + 1;
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(json);
CkStringBuilderW_Dispose(sbResponseBody);
CkJsonObjectW_Dispose(jResp);
}
Curl Command
curl -X POST
-H "Content-Type: application/json"
-H "Accept: application/json"
-H "Authorization: Bearer {{init_access_token}}"
-d '{
"redirect_uris": [
"http://localhost"
],
"response_types": [
"code",
"token",
"id_token"
],
"grant_types": [
"authorization_code",
"implicit",
"refresh_token"
],
"application_type": "web",
"contacts": [
"abc@sf.com",
"ve7jtb@example.org"
],
"client_name": "Example Olivier"
}'
https://login.salesforce.com{{site}}/services/oauth2/register
Postman Collection Item JSON
{
"name": "OpenID Connect Dynamic Client Registration Endpoint",
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
},
{
"key": "Accept",
"value": "application/json",
"type": "text"
},
{
"key": "Authorization",
"value": "Bearer {{init_access_token}}",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"redirect_uris\": [\n \"http://localhost\"\n ],\n \"response_types\": [\n \"code\",\n \"token\",\n \"id_token\"\n ],\n \"grant_types\": [\n \"authorization_code\",\n \"implicit\",\n \"refresh_token\"\n ],\n \"application_type\": \"web\",\n \"contacts\": [\n \"abc@sf.com\",\n \"ve7jtb@example.org\"\n ],\n \"client_name\": \"Example Olivier\"\n}"
},
"url": {
"raw": "{{url}}{{site}}/services/oauth2/register",
"host": [
"{{url}}{{site}}"
],
"path": [
"services",
"oauth2",
"register"
]
}
},
"response": [
{
"name": "Successful OpenID Connect Dynamic Client Registration Endpoint",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Accept",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer {{init_access_token}}"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"redirect_uris\": [\n \"http://localhost\"\n ],\n \"response_types\": [\n \"code\",\n \"token\",\n \"id_token\"\n ],\n \"grant_types\": [\n \"authorization_code\",\n \"implicit\",\n \"refresh_token\"\n ],\n \"application_type\": \"web\",\n \"contacts\": [\n \"abc@sf.com\",\n \"ve7jtb@example.org\"\n ],\n \"client_name\": \"Sample Customer\"\n}"
},
"url": {
"raw": "{{url}}{{site}}/services/oauth2/register",
"host": [
"{{url}}{{site}}"
],
"path": [
"services",
"oauth2",
"register"
]
}
},
"status": "Created",
"code": 201,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Thu, 16 Nov 2023 16:04:08 GMT"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=63072000; includeSubDomains"
},
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "X-XSS-Protection",
"value": "1; mode=block"
},
{
"key": "X-Robots-Tag",
"value": "none"
},
{
"key": "Cache-Control",
"value": "no-cache,must-revalidate,max-age=0,no-store,private"
},
{
"key": "Expires",
"value": "Thu, 01 Jan 1970 00:00:00 GMT"
},
{
"key": "Content-Type",
"value": "application/json;charset=UTF-8"
},
{
"key": "Vary",
"value": "Accept-Encoding"
},
{
"key": "Content-Encoding",
"value": "gzip"
},
{
"key": "Transfer-Encoding",
"value": "chunked"
}
],
"cookie": [
],
"body": "{\n \"client_id\": \"<client_id>\",\n \"client_secret\": \"<client_secret>\",\n \"registration_access_token\": \"<token>\",\n \"registration_client_uri\": \"https://login.salesforce.com/services/oauth2/register/<client_id>\",\n \"client_id_issued_at\": <timestamp>,\n \"client_secret_expires_at\": 0,\n \"token_endpoint_auth_method\": \"client_secret_post\",\n \"redirect_uris\": [\n \"http://localhost\"\n ],\n \"response_types\": [\n \"code\",\n \"token\",\n \"id_token\"\n ],\n \"grant_types\": [\n \"authorization_code\",\n \"hybrid_auth_code\",\n \"implicit\",\n \"refresh_token\"\n ],\n \"contacts\": [\n \"abc@sf.com\"\n ],\n \"client_name\": \"Sample Customer\",\n \"scopes\": [\n \"openid\",\n \"refresh_token\",\n \"id\",\n \"api\"\n ]\n}"
}
]
}