Chilkat Online Tools

Node.js / Orchestrator API Documentation / Authenticate (OnPrem)

Back to Collection Items

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node21-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node21-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node21-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node21-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node21-linux64');
    }
} else if (os.platform() == 'darwin') {
    if (os.arch() == 'arm64') {
        var chilkat = require('@chilkat/ck-node21-mac-m1');
    } else {
        var chilkat = require('@chilkat/ck-node21-macosx');
    }
}


function chilkatExample() {

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    var http = new chilkat.Http();
    var success;

    // Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "password": "{{password}}",
    //   "usernameOrEmailAddress": "{{usernameOrEmailAddress}}",
    //   "tenancyName": "{{tenant}}"
    // }

    var json = new chilkat.JsonObject();
    json.UpdateString("password","{{password}}");
    json.UpdateString("usernameOrEmailAddress","{{usernameOrEmailAddress}}");
    json.UpdateString("tenancyName","{{tenant}}");

    // Adds the "Authorization: Bearer <access_token>" header.
    http.AuthToken = "<access_token>";

    // resp: HttpResponse
    var resp = http.PostJson3("https://domain.com/api/Account/Authenticate","application/json",json);
    if (http.LastMethodSuccess == false) {
        console.log(http.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    resp.GetBodySb(sbResponseBody);

    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = resp.StatusCode;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(resp.Header);
        console.log("Failed.");

        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "result": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjVENzg4QTgyMEE4QzAzMkJBNzg1Mjk4QjNBMTlEMkNGMEQyODI2ODYiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJYWGlLZ2dxTUF5dW5oU21MT2huU3p3MG9Kb1kifQ.eyJuYmYiOjE2MTc5NzQ1NjYsImV4cCI6MTYxNzk3ODQ2NiwiaXNzIjoiaHR0cHM6Ly9wdGFvcmNoMjAxMC1pcy5henVyZXdlYnNpdGVzLm5ldC9pZGVudGl0eSIsImF1ZCI6Ik9yY2hlc3RyYXRvckFwaVVzZXJBY2Nlc3MiLCJjbGllbnRfaWQiOiI0ZDdmMmJmMi05MDkzLTQ5NTktOWQ1Mi1iNGE2NjBmMTQ4NWYiLCJzdWIiOiJkNGFiODFlNi00NjU5LTRjMjUtOGFjNy0wYTQ4MGFhZTE3ZjAiLCJhdXRoX3RpbWUiOjE2MTc5NzQ4NjYsImlkcCI6ImxvY2FsIiwiQXNwTmV0LklkZW50aXR5LlNlY3VyaXR5U3RhbXAiOiJTWFoyTUFJV0wzSUxBNUtLWVZBM1pWWFdURERKRVU2WCIsInBydF9pZCI6IjNkZDg0NGQwLWZjY2YtMGUyZC04ZTE4LWMyZTNiMzExYTBhZiIsImhvc3QiOiJGYWxzZSIsImZpcnN0X25hbWUiOiJDb2RlIFNjaWVuY2UiLCJsYXN0X25hbWUiOiJRQSIsInBydF9hZG0iOiJGYWxzZSIsInByZWZlcnJlZF91c2VybmFtZSI6ImNvZGVzY2llbmNlcWEiLCJuYW1lIjoiY29kZXNjaWVuY2VxYSIsInNjb3BlIjpbImVtYWlsIiwib3BlbmlkIiwicHJvZmlsZSIsInRlbmFudCIsIk9yY2hlc3RyYXRvckFwaVVzZXJBY2Nlc3MiXSwiYW1yIjpbInB3ZCJdfQ.rQfDqz83R5qujADvcEBhSKREDCrW_L3LwCeRDlSf1NIpIykyCjTdprPp7QgbOmB5FRf7H1778ZY3pkPY4C_EH60lTqraNKe66E09vTpRPlASQoWrbUprNSxkVd7KLTQ16QUthelVsml_D39uSCBfhiUJR0N4Bpr9GF5IVsGOuRgwQtXiw4Gi-7hlWBX5qhnj3GO29uVO5oiSJJtKlMTqWO8iIZloURFdVxYUpXuywH6nEmeUFdIvOsUcFKjPOkpUW2SdIPqWjy7yTplAXoXny1yKBfPbEkwtivvOrzRz6fz6rPQO8qNTKcAf28oeO2iLYWRN7u4AIKLgPdpkbXjwgrrVXgY3Duvw6yjBsC2Yt9Iu6DJMXq2ereBA9E_PgiMTv7D3NbKcyqLUNmTScVfWBM92qMw9UzKh1_fl5qjN3DtfdRWek8AJowxkgDskNScemdkemXAS2cGSgFc7JxEsxmZWa8CSxuSuVw9XSsFc37GaZo3t2XusJR8P55frEzcycfnxfDOJfp8iFmaMY9slUiAXX1Y4lo6_LJBBld1_y_AxRzHBskvsNc5WZwFCfrMM27NjFATfrODToECkWq-S3s-rI6h-mjQZECta4PG9cDYK5cyPB6yYp9VY2FqMg4znUIphyQIVRgHUsC2cGSo-UGZdCrgX5J4za5b3ze9OoAY",
    //   "targetUrl": null,
    //   "success": true,
    //   "error": null,
    //   "unAuthorizedRequest": false,
    //   "__abp": true
    // }

    // Sample code for parsing the JSON response...
    // Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    var result = jResp.StringOf("result");
    var targetUrl = jResp.StringOf("targetUrl");
    var success = jResp.BoolOf("success");
    var error = jResp.StringOf("error");
    var unAuthorizedRequest = jResp.BoolOf("unAuthorizedRequest");
    var v__abp = jResp.BoolOf("__abp");

}

chilkatExample();

Curl Command

curl -X POST
	-H "Authorization: Bearer <access_token>"
	-d '{
    "password": "{{password}}",
    "usernameOrEmailAddress": "{{usernameOrEmailAddress}}",
    "tenancyName": "{{tenant}}"
}'
https://domain.com/api/Account/Authenticate

Postman Collection Item JSON

{
  "name": "Authenticate (OnPrem)",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "//test status code\r",
          "pm.test(\"Status code is 200\", function () {\r",
          "    pm.response.to.have.status(200);\r",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n    \"password\": \"{{password}}\",\r\n    \"usernameOrEmailAddress\": \"{{usernameOrEmailAddress}}\",\r\n    \"tenancyName\": \"{{tenant}}\"\r\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{OnPremUrl}}/api/Account/Authenticate",
      "host": [
        "{{OnPremUrl}}"
      ],
      "path": [
        "api",
        "Account",
        "Authenticate"
      ]
    }
  },
  "response": [
    {
      "name": "Authenticate (OnPrem)",
      "originalRequest": {
        "method": "POST",
        "header": [
        ],
        "body": {
          "mode": "raw",
          "raw": "{\r\n    \"password\": \"{{password}}\",\r\n    \"usernameOrEmailAddress\": \"{{usernameOrEmailAddress}}\",\r\n    \"tenancyName\": \"{{tenant}}\"\r\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{OnPremUrl}}/api/Account/Authenticate",
          "host": [
            "{{OnPremUrl}}"
          ],
          "path": [
            "api",
            "Account",
            "Authenticate"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Cache-Control",
          "value": "no-cache, no-store, must-revalidate"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip"
        },
        {
          "key": "Expires",
          "value": "-1"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding"
        },
        {
          "key": "Request-Context",
          "value": "appId=cid-v1:947add8d-3c5d-4dda-a0ab-9f714fdafac3"
        },
        {
          "key": "X-Robots-Tag",
          "value": "noindex, nofollow"
        },
        {
          "key": "X-Download-Options",
          "value": "noopen"
        },
        {
          "key": "X-XSS-Protection",
          "value": "1"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-Frame-Options",
          "value": "Deny"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains"
        },
        {
          "key": "Content-Security-Policy",
          "value": "default-src 'self';script-src 'self';style-src 'self' 'unsafe-inline';img-src 'self' data: https://*.blob.core.windows.net https://*.amazonaws.com blob:;font-src 'self' data:;connect-src 'self' wss: https://sentry.io https://studio-feedback.azure-api.net https://*.service.signalr.net https://*.blob.core.windows.net https://*.amazonaws.com dc.services.visualstudio.com;worker-src 'self' blob:"
        },
        {
          "key": "X-Correlation-ID",
          "value": "b707aed0-bdf0-4a36-a74f-e87176e3d256"
        },
        {
          "key": "api-supported-versions",
          "value": "11.0"
        },
        {
          "key": "Date",
          "value": "Fri, 09 Apr 2021 13:27:46 GMT"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"result\": \"eyJhbGciOiJSUzI1NiIsImtpZCI6IjVENzg4QTgyMEE4QzAzMkJBNzg1Mjk4QjNBMTlEMkNGMEQyODI2ODYiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJYWGlLZ2dxTUF5dW5oU21MT2huU3p3MG9Kb1kifQ.eyJuYmYiOjE2MTc5NzQ1NjYsImV4cCI6MTYxNzk3ODQ2NiwiaXNzIjoiaHR0cHM6Ly9wdGFvcmNoMjAxMC1pcy5henVyZXdlYnNpdGVzLm5ldC9pZGVudGl0eSIsImF1ZCI6Ik9yY2hlc3RyYXRvckFwaVVzZXJBY2Nlc3MiLCJjbGllbnRfaWQiOiI0ZDdmMmJmMi05MDkzLTQ5NTktOWQ1Mi1iNGE2NjBmMTQ4NWYiLCJzdWIiOiJkNGFiODFlNi00NjU5LTRjMjUtOGFjNy0wYTQ4MGFhZTE3ZjAiLCJhdXRoX3RpbWUiOjE2MTc5NzQ4NjYsImlkcCI6Im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Yp9VY2FqMg4znUIphyQIVRgHUsC2cGSo-UGZdCrgX5J4za5b3ze9OoAY\",\n    \"targetUrl\": null,\n    \"success\": true,\n    \"error\": null,\n    \"unAuthorizedRequest\": false,\n    \"__abp\": true\n}"
    }
  ]
}