Back to Collection Items
IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttpRequest.pb"
Procedure ChilkatExample()
; This example assumes the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success.i
req.i = CkHttpRequest::ckCreate()
If req.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpRequest::ckAddParam(req,"grant_type","urn:ietf:params:oauth:grant-type:token-exchange")
CkHttpRequest::ckAddParam(req,"subject_token_type","urn:ietf:params:oauth:token-type:access_token")
jsonParam3.i = CkJsonObject::ckCreate()
If jsonParam3.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpRequest::ckAddParam(req,"subject_token",CkJsonObject::ckEmit(jsonParam3))
CkHttpRequest::ckAddParam(req,"actor_token_type","urn:ietf:params:oauth:token-type:jwt")
CkHttpRequest::ckAddParam(req,"actor_token","")
resp.i = CkHttp::ckPostUrlEncoded(http,"https://login.salesforce.com{{site}}/services/oauth2/token",req)
If CkHttp::ckLastMethodSuccess(http) = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkJsonObject::ckDispose(jsonParam3)
ProcedureReturn
EndIf
sbResponseBody.i = CkStringBuilder::ckCreate()
If sbResponseBody.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
jResp.i = CkJsonObject::ckCreate()
If jResp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkJsonObject::ckLoadSb(jResp,sbResponseBody)
CkJsonObject::setCkEmitCompact(jResp, 0)
Debug "Response Body:"
Debug CkJsonObject::ckEmit(jResp)
respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
Debug "Response Status Code = " + Str(respStatusCode)
If respStatusCode >= 400
Debug "Response Header:"
Debug CkHttpResponse::ckHeader(resp)
Debug "Failed."
CkHttpResponse::ckDispose(resp)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkJsonObject::ckDispose(jsonParam3)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndIf
CkHttpResponse::ckDispose(resp)
; Sample JSON response:
; (Sample code for parsing the JSON response is shown below)
; {
; "access_token": "<access_token>",
; "issued_token_type": "urn:ietf:params:oauth:token-type:jwt",
; "token_type": "Bearer",
; "expires_in": 60
; }
; Sample code for parsing the JSON response...
; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code
access_token.s = CkJsonObject::ckStringOf(jResp,"access_token")
issued_token_type.s = CkJsonObject::ckStringOf(jResp,"issued_token_type")
token_type.s = CkJsonObject::ckStringOf(jResp,"token_type")
expires_in.i = CkJsonObject::ckIntOf(jResp,"expires_in")
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkJsonObject::ckDispose(jsonParam3)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure
Curl Command
curl -X POST
-H "Content-Type: application/x-www-form-urlencoded"
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange'
--data-urlencode 'subject_token_type=urn:ietf:params:oauth:token-type:access_token'
--data-urlencode 'subject_token={{_accessToken}}'
--data-urlencode 'actor_token_type=urn:ietf:params:oauth:token-type:jwt'
--data-urlencode 'actor_token='
https://login.salesforce.com{{site}}/services/oauth2/token
Postman Collection Item JSON
{
"name": "Asset Token Flow",
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/x-www-form-urlencoded",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "urn:ietf:params:oauth:grant-type:token-exchange",
"type": "text"
},
{
"key": "subject_token_type",
"value": "urn:ietf:params:oauth:token-type:access_token",
"type": "text"
},
{
"key": "subject_token",
"value": "{{_accessToken}}",
"type": "text"
},
{
"key": "actor_token_type",
"value": "urn:ietf:params:oauth:token-type:jwt",
"type": "text"
},
{
"key": "actor_token",
"value": "",
"type": "text"
}
]
},
"url": {
"raw": "{{url}}{{site}}/services/oauth2/token",
"host": [
"{{url}}{{site}}"
],
"path": [
"services",
"oauth2",
"token"
]
}
},
"response": [
{
"name": "Successful Asset Token Flow",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "urn:ietf:params:oauth:grant-type:token-exchange",
"type": "text"
},
{
"key": "subject_token_type",
"value": "urn:ietf:params:oauth:token-type:access_token",
"type": "text"
},
{
"key": "subject_token",
"value": "{{_accessToken}}",
"type": "text"
},
{
"key": "actor_token_type",
"value": "urn:ietf:params:oauth:token-type:jwt",
"type": "text"
},
{
"key": "actor_token",
"value": "",
"type": "text"
}
]
},
"url": {
"raw": "{{url}}{{site}}/services/oauth2/token",
"host": [
"{{url}}{{site}}"
],
"path": [
"services",
"oauth2",
"token"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Thu, 16 Nov 2023 15:52:11 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": "X-ReadOnlyMode",
"value": "false"
},
{
"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 \"access_token\": \"<access_token>\",\n \"issued_token_type\": \"urn:ietf:params:oauth:token-type:jwt\",\n \"token_type\": \"Bearer\",\n \"expires_in\": 60\n}"
}
]
}