Chilkat Online Tools

ERROR!

------------------- GenerateCode ----------------------
ImpliedContentType: application/x-www-form-urlencoded
explicitContentType: 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;
call req.AddParam("grant_type","password");

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

new JsonObject jsonParam3;
call req.AddParam("client_secret",jsonParam3.Emit());

new JsonObject jsonParam4;
call req.AddParam("username",jsonParam4.Emit());

new JsonObject jsonParam5;
call req.AddParam("password",jsonParam5.Emit());

call req.AddHeader("Accept","application/json");

new HttpResponse resp;
req.HttpVerb = "POST";
req.ContentType = "application/x-www-form-urlencoded";
success = http.PostUrlEncoded("https://login.salesforce.com{{site}}/services/oauth2/token", 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)

// {
//   "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: {{.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


string access_token = jResp.StringOf("access_token");
string instance_url = jResp.StringOf("instance_url");
string id = jResp.StringOf("id");
string token_type = jResp.StringOf("token_type");
string issued_at = jResp.StringOf("issued_at");
string signature = jResp.StringOf("signature");



---- end chilkat script ----

TCL / Salesforce Platform APIs / Username Password Flow

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]

CkHttpRequest_AddParam $req "grant_type" "password"

set jsonParam2 [new_CkJsonObject]

CkHttpRequest_AddParam $req "client_id" [CkJsonObject_emit $jsonParam2]

set jsonParam3 [new_CkJsonObject]

CkHttpRequest_AddParam $req "client_secret" [CkJsonObject_emit $jsonParam3]

set jsonParam4 [new_CkJsonObject]

CkHttpRequest_AddParam $req "username" [CkJsonObject_emit $jsonParam4]

set jsonParam5 [new_CkJsonObject]

CkHttpRequest_AddParam $req "password" [CkJsonObject_emit $jsonParam5]

CkHttpRequest_AddHeader $req "Accept" "application/json"

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 $jsonParam2
    delete_CkJsonObject $jsonParam3
    delete_CkJsonObject $jsonParam4
    delete_CkJsonObject $jsonParam5
    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 $jsonParam2
    delete_CkJsonObject $jsonParam3
    delete_CkJsonObject $jsonParam4
    delete_CkJsonObject $jsonParam5
    delete_CkHttpResponse $resp
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# 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

set access_token [CkJsonObject_stringOf $jResp "access_token"]
set instance_url [CkJsonObject_stringOf $jResp "instance_url"]
set id [CkJsonObject_stringOf $jResp "id"]
set token_type [CkJsonObject_stringOf $jResp "token_type"]
set issued_at [CkJsonObject_stringOf $jResp "issued_at"]
set signature [CkJsonObject_stringOf $jResp "signature"]

delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkJsonObject $jsonParam2
delete_CkJsonObject $jsonParam3
delete_CkJsonObject $jsonParam4
delete_CkJsonObject $jsonParam5
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $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}"
    }
  ]
}