Back to Collection Items
#include <C_CkHttp.h>
#include <C_CkHttpRequest.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
HCkHttp http;
BOOL success;
HCkHttpRequest req;
HCkJsonObject jsonParam2;
HCkJsonObject jsonParam3;
HCkJsonObject jsonParam4;
HCkJsonObject jsonParam5;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *access_token;
const char *instance_url;
const char *id;
const char *token_type;
const char *issued_at;
const char *signature;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
req = CkHttpRequest_Create();
CkHttpRequest_AddParam(req,"grant_type","password");
jsonParam2 = CkJsonObject_Create();
CkHttpRequest_AddParam(req,"client_id",CkJsonObject_emit(jsonParam2));
jsonParam3 = CkJsonObject_Create();
CkHttpRequest_AddParam(req,"client_secret",CkJsonObject_emit(jsonParam3));
jsonParam4 = CkJsonObject_Create();
CkHttpRequest_AddParam(req,"username",CkJsonObject_emit(jsonParam4));
jsonParam5 = CkJsonObject_Create();
CkHttpRequest_AddParam(req,"password",CkJsonObject_emit(jsonParam5));
CkHttpRequest_AddHeader(req,"Accept","application/json");
resp = CkHttp_PostUrlEncoded(http,"https://login.salesforce.com{{site}}/services/oauth2/token",req);
if (CkHttp_getLastMethodSuccess(http) == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkJsonObject_Dispose(jsonParam2);
CkJsonObject_Dispose(jsonParam3);
CkJsonObject_Dispose(jsonParam4);
CkJsonObject_Dispose(jsonParam5);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttpResponse_Dispose(resp);
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkJsonObject_Dispose(jsonParam2);
CkJsonObject_Dispose(jsonParam3);
CkJsonObject_Dispose(jsonParam4);
CkJsonObject_Dispose(jsonParam5);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
CkHttpResponse_Dispose(resp);
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "access_token": "ACCESS_TOKEN_GOES_HERE",
// "instance_url": "https://pozil-dev-ed.my.salesforce.com",
// "id": "https://login.salesforce.com/id/00D58000000arpqEAA/00558000000yFyDAAU",
// "token_type": "Bearer",
// "issued_at": "1609942615640",
// "signature": "SIGNATURE_GOES_HERE"
// }
// 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.
access_token = CkJsonObject_stringOf(jResp,"access_token");
instance_url = CkJsonObject_stringOf(jResp,"instance_url");
id = CkJsonObject_stringOf(jResp,"id");
token_type = CkJsonObject_stringOf(jResp,"token_type");
issued_at = CkJsonObject_stringOf(jResp,"issued_at");
signature = CkJsonObject_stringOf(jResp,"signature");
CkHttp_Dispose(http);
CkHttpRequest_Dispose(req);
CkJsonObject_Dispose(jsonParam2);
CkJsonObject_Dispose(jsonParam3);
CkJsonObject_Dispose(jsonParam4);
CkJsonObject_Dispose(jsonParam5);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}
Curl Command
curl -X POST
-H "Content-Type: application/x-www-form-urlencoded"
-H "Accept: application/json"
--data-urlencode 'grant_type=password'
--data-urlencode 'client_id={{clientId}}'
--data-urlencode 'client_secret={{clientSecret}}'
--data-urlencode 'username={{username}}'
--data-urlencode 'password={{password}}{{secretToken}}'
https://login.salesforce.com{{site}}/services/oauth2/token
Postman Collection Item JSON
{
"name": "Username Password Flow",
"event": [
{
"listen": "test",
"script": {
"exec": [
"const jsonData = pm.response.json();",
"const id = jsonData.id.split('/');",
"",
"const context = pm.environment.name ? pm.environment : pm.collectionVariables;",
"context.set(\"_accessToken\", jsonData.access_token);",
"context.set(\"_endpoint\", jsonData.instance_url);",
"context.set(\"_userId\", id.pop());",
"context.set(\"_orgId\", id.pop());",
""
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
},
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "password",
"type": "text"
},
{
"key": "client_id",
"value": "{{clientId}}",
"type": "text"
},
{
"key": "client_secret",
"value": "{{clientSecret}}",
"type": "text"
},
{
"key": "username",
"value": "{{username}}",
"type": "text"
},
{
"key": "password",
"value": "{{password}}{{secretToken}}",
"type": "text"
}
]
},
"url": {
"raw": "{{url}}{{site}}/services/oauth2/token",
"host": [
"{{url}}{{site}}"
],
"path": [
"services",
"oauth2",
"token"
]
}
},
"response": [
{
"name": "Successful OAuth Username Password Login",
"originalRequest": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
},
{
"key": "Accept",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "password",
"type": "text"
},
{
"key": "client_id",
"value": "CLIENT_ID_GOES_HERE",
"type": "text"
},
{
"key": "client_secret",
"value": "CLIENT_SECRET_GOES_HERE",
"type": "text"
},
{
"key": "username",
"value": "demo@pozil.org",
"type": "text"
},
{
"key": "password",
"value": "PASSWORD_AND_SECURITY_TOKEN_GO_HERE",
"type": "text"
}
]
},
"url": {
"raw": "https://login.salesforce.com/services/oauth2/token",
"protocol": "https",
"host": [
"login",
"salesforce",
"com"
],
"path": [
"services",
"oauth2",
"token"
]
}
},
"status": "OK",
"code": 200,
"_postman_previewlanguage": "json",
"header": [
{
"key": "Date",
"value": "Wed, 06 Jan 2021 14:16:55 GMT"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=31536002; includeSubDomains"
},
{
"key": "Public-Key-Pins-Report-Only",
"value": "pin-sha256=\"9n0izTnSRF+W4W4WhQB8duS2bxVLfzXsY=\"; pin-sha256=\"5kJvNEMw0KjrCAu7eXY5HZdvSP91w=\"; pin-sha256=\"njN4rRG+zPUPHlv4+foULwl1g=\"; max-age=86400; includeSubDomains; report-uri=\"https://a.forcesslreports.com/hpkp-report/00D58000000arpqm\";"
},
{
"key": "Expect-CT",
"value": "max-age=86400, report-uri=\"https://a.forcesslreports.com/Expect-CT-report/00D58000000arpqm\""
},
{
"key": "X-Robots-Tag",
"value": "none"
},
{
"key": "X-B3-TraceId",
"value": "c21e47e40ecc875d"
},
{
"key": "X-B3-SpanId",
"value": "c21e47e40ecc875d"
},
{
"key": "X-B3-Sampled",
"value": "0"
},
{
"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_GOES_HERE\",\n \"instance_url\": \"https://pozil-dev-ed.my.salesforce.com\",\n \"id\": \"https://login.salesforce.com/id/00D58000000arpqEAA/00558000000yFyDAAU\",\n \"token_type\": \"Bearer\",\n \"issued_at\": \"1609942615640\",\n \"signature\": \"SIGNATURE_GOES_HERE\"\n}"
}
]
}