Chilkat Online Tools

SQL Server / Orchestrator / Authenticate (On-Premise)

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

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

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

    -- {
    --   "tenancyName": "{{tenantName}}",
    --   "usernameOrEmailAddress": "{{usernameOrEmailAddress}}",
    --   "password": "{{password}}"
    -- }

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'tenancyName', '{{tenantName}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'usernameOrEmailAddress', '{{usernameOrEmailAddress}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'password', '{{password}}'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'User-Agent', ''

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/api/Account/Authenticate', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @resp

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

    -- {
    --   "result": "ND_kEaqVx4__SQ0zNEA3vV15ovYqXvpHnpYEnbKKHSjcc0ENIrOzgmj67uXOAxq--lyAvyUMIRp0i0XAAIOQHA_ekFz6idQBCYFKvRTWttYs8xD1pRRIP-pchsbJYhI-QVxrrd0H7PXourk6y0Nt049qWYP-2XrU6oziEX7usl6zK45pOQ4Iut5nmOFQsnd5IMIGBRmdH0fh3hbCtSgQ22S28a44PZHeRCIqb5gKMQKZ0UGvl4sgvw6EFygybPajmPKHQ-oAzNpXamMeicq7y2AM7wJWoUIalE7O0Z3DPogi0U47CqtVVmJfQ4R7OoNM8MhIlqfPoN-IEIULPll4fgPDAgY54iYMYLbXgrFBjSEC1lGMpkTO-40xBbIjoBeL0OV3mVazgZcysqQ7BcwvtpRnZX_VK4V_Pwhzt4ZItsCFS8yOQM0FblT6LBn87JZlQ3nx9FIEo9pny0kHo9v9QJ_iU9nsFNPB71qa1JU6apLBhq5Rj7_PSrm0CfPOizIXRuq0GZ2uXdseR0Xk79-123",
    --   "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

    DECLARE @result nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @result OUT, 'result'
    DECLARE @targetUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @targetUrl OUT, 'targetUrl'
    DECLARE @success int
    EXEC sp_OAMethod @jResp, 'BoolOf', @success OUT, 'success'
    DECLARE @error nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @error OUT, 'error'
    DECLARE @unAuthorizedRequest int
    EXEC sp_OAMethod @jResp, 'BoolOf', @unAuthorizedRequest OUT, 'unAuthorizedRequest'
    DECLARE @v__abp int
    EXEC sp_OAMethod @jResp, 'BoolOf', @v__abp OUT, '__abp'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "User-Agent: "
	-d '{
	"tenancyName": "{{tenantName}}",
	"usernameOrEmailAddress": "{{usernameOrEmailAddress}}",
	"password": "{{password}}"
}'
https://domain.com/api/Account/Authenticate

Postman Collection Item JSON

{
  "name": "Authenticate (On-Premise)",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "pm.test(\"Token Saved\", function () {",
          "    var oldToken = pm.environment.get(\"token\");",
          "    var jsonData = pm.response.json();",
          "    ",
          "    pm.environment.set(\"token\", jsonData.result);",
          "    pm.expect(pm.environment.get(\"token\")).to.not.equal(oldToken);",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "User-Agent",
        "value": "",
        "disabled": true
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n\t\"tenancyName\": \"{{tenantName}}\",\n\t\"usernameOrEmailAddress\": \"{{usernameOrEmailAddress}}\",\n\t\"password\": \"{{password}}\"\n}"
    },
    "url": {
      "raw": "{{url}}/api/Account/Authenticate",
      "host": [
        "{{url}}"
      ],
      "path": [
        "api",
        "Account",
        "Authenticate"
      ]
    },
    "description": "A method for manually retrieving an authentication token using the login credentials supplied in the Body. This token is then made available to all other calls."
  },
  "response": [
    {
      "name": "Authenticate",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json",
            "disabled": false
          },
          {
            "key": "User-Agent",
            "value": "",
            "disabled": true
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n\t\"tenancyName\": \"{{tenantName}}\",\n\t\"usernameOrEmailAddress\": \"{{usernameOrEmailAddress}}\",\n\t\"password\": \"{{password}}\"\n}"
        },
        "url": {
          "raw": "{{url}}/api/Account/Authenticate",
          "host": [
            "{{url}}"
          ],
          "path": [
            "api",
            "Account",
            "Authenticate"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "CF-RAY",
          "value": "43b30aa3fdd1bf07-FRA",
          "name": "CF-RAY",
          "description": "Custom header"
        },
        {
          "key": "Cache-Control",
          "value": "no-store, must-revalidate, no-cache, max-age=0",
          "name": "Cache-Control",
          "description": "Tells all caching mechanisms from server to client whether they may cache this object. It is measured in seconds"
        },
        {
          "key": "Connection",
          "value": "keep-alive",
          "name": "Connection",
          "description": "Options that are desired for the connection"
        },
        {
          "key": "Content-Encoding",
          "value": "gzip",
          "name": "Content-Encoding",
          "description": "The type of encoding used on the data."
        },
        {
          "key": "Content-Length",
          "value": "798",
          "name": "Content-Length",
          "description": "The length of the response body in octets (8-bit bytes)"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8",
          "name": "Content-Type",
          "description": "The mime type of this content"
        },
        {
          "key": "Date",
          "value": "Mon, 16 Jul 2018 08:19:02 GMT",
          "name": "Date",
          "description": "The date and time that the message was sent"
        },
        {
          "key": "Expect-CT",
          "value": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
          "name": "Expect-CT",
          "description": "Custom header"
        },
        {
          "key": "Server",
          "value": "cloudflare",
          "name": "Server",
          "description": "A name for the server"
        },
        {
          "key": "Set-Cookie",
          "value": "; SameSite=lax",
          "name": "Set-Cookie",
          "description": "an HTTP cookie"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000; includeSubDomains",
          "name": "Strict-Transport-Security",
          "description": "A HSTS Policy informing the HTTP client how long to cache the HTTPS only policy and whether this applies to subdomains."
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding",
          "name": "Vary",
          "description": "Tells downstream proxies how to match future request headers to decide whether the cached response can be used rather than requesting a fresh one from the origin server."
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff",
          "name": "X-Content-Type-Options",
          "description": "The only defined value, \"nosniff\", prevents Internet Explorer from MIME-sniffing a response away from the declared content-type"
        },
        {
          "key": "X-Frame-Options",
          "value": "DENY",
          "name": "X-Frame-Options",
          "description": "Clickjacking protection: \"deny\" - no rendering within a frame, \"sameorigin\" - no rendering if origin mismatch"
        },
        {
          "key": "api-supported-versions",
          "value": "5.0",
          "name": "api-supported-versions",
          "description": "Custom header"
        }
      ],
      "cookie": [
        {
          "expires": "Wed Dec 19 2018 11:35:02 GMT-0600 (Central Standard Time)",
          "httpOnly": true,
          "domain": "uipath.com",
          "path": "/",
          "secure": true,
          "value": "d3b89a558feb68050f4b847c971f595771513704902",
          "key": "__cfduid"
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/QueueProcessingRecords",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/api/RobotsService",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Processes",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Settings",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Robots",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Queues",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Assets",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/api/Account",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Users",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata/Jobs",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/api/Logs",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/Account",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/odata",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/api",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/",
          "secure": false,
          "value": "",
          "key": ""
        },
        {
          "expires": "Mon Jan 18 2038 21:14:07 GMT-0600 (Central Standard Time)",
          "httpOnly": false,
          "domain": "platform.uipath.com",
          "path": "/",
          "secure": false,
          "value": "GC-osp2U-YDhoXTOqXnUp3DaN1cN_siVCaNVue_jljOcqJLYHQrRk5msz52KHL8XauydRYx_3JyjLtLDpItypCPdEL20jFnxlF2g-f86Qk41",
          "key": "__RequestVerificationToken"
        }
      ],
      "body": "{\n    \"result\": \"ND_kEaqVx4__SQ0zNEA3vV15ovYqXvpHnpYEnbKKHSjcc0ENIrOzgmj67uXOAxq--lyAvyUMIRp0i0XAAIOQHA_ekFz6idQBCYFKvRTWttYs8xD1pRRIP-pchsbJYhI-QVxrrd0H7PXourk6y0Nt049qWYP-2XrU6oziEX7usl6zK45pOQ4Iut5nmOFQsnd5IMIGBRmdH0fh3hbCtSgQ22S28a44PZHeRCIqb5gKMQKZ0UGvl4sgvw6EFygybPajmPKHQ-oAzNpXamMeicq7y2AM7wJWoUIalE7O0Z3DPogi0U47CqtVVmJfQ4R7OoNM8MhIlqfPoN-IEIULPll4fgPDAgY54iYMYLbXgrFBjSEC1lGMpkTO-40xBbIjoBeL0OV3mVazgZcysqQ7BcwvtpRnZX_VK4V_Pwhzt4ZItsCFS8yOQM0FblT6LBn87JZlQ3nx9FIEo9pny0kHo9v9QJ_iU9nsFNPB71qa1JU6apLBhq5Rj7_PSrm0CfPOizIXRuq0GZ2uXdseR0Xk79-123\",\n    \"targetUrl\": null,\n    \"success\": true,\n    \"error\": null,\n    \"unAuthorizedRequest\": false,\n    \"__abp\": true\n}"
    }
  ]
}