Chilkat Online Tools

SQL Server / Orchestrator / 1a. Authenticate (Cloud)

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
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.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.

    -- {
    --   "grant_type": "refresh_token",
    --   "client_id": "{{clientId}}",
    --   "refresh_token": "{{userKey}}"
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'grant_type', 'refresh_token'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'client_id', '{{clientId}}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'refresh_token', '{{userKey}}'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'X-UIPATH-TenantName', '{{tenantName}}'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://account.uipath.com/oauth/token', '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

    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    PRINT @iTmp0
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    PRINT @sTmp0
    EXEC @hr = sp_OADestroy @resp


    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json


END
GO

Curl Command

curl -X POST
	-H "Content-Type: application/json"
	-H "X-UIPATH-TenantName: {{tenantName}}"
	-d '{
    "grant_type": "refresh_token",
    "client_id": "{{clientId}}",
    "refresh_token": "{{userKey}}"
}'
https://account.uipath.com/oauth/token

Postman Collection Item JSON

{
  "name": "1a. Authenticate (Cloud)",
  "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.access_token);",
          "    pm.expect(pm.environment.get(\"token\")).to.not.equal(oldToken);",
          "});"
        ],
        "type": "text/javascript"
      }
    },
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "function GetTenant(url) {",
          "    var pieces = url.split('/');",
          "    ",
          "    // Check if user formatted the url correctly",
          "    if (pieces.length < 5)",
          "        throw \"Detected an invalid Cloud URL. Your URL must be in the form 'https://<platform>.uipath.com/<account logical name>/<tenant logical name>\";",
          "        ",
          "    // Return the last slug, which will be the tenant name",
          "    return pieces.pop();",
          "}",
          "    ",
          "// Get URL",
          "var url = pm.environment.get(\"url\");",
          "",
          "// Check if cloud URL",
          "if (url.includes(\"uipath.com\")) {",
          "    // Extract tenant from URL",
          "    var tenant = GetTenant(url);",
          "    pm.environment.set(\"tenantName\", tenant);",
          "}",
          "",
          "// Throw error if url is incorrect",
          "else {",
          "    throw \"This authentication method may only be used with Cloud Orchestrators (i.e. those hosted by uipath.com. Please change your URL (\" + url + \") if you intend to use a Cloud-hosted Orchestrator.\";   ",
          "}"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "X-UIPATH-TenantName",
        "value": "{{tenantName}}",
        "type": "text"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\r\n    \"grant_type\": \"refresh_token\",\r\n    \"client_id\": \"{{clientId}}\",\r\n    \"refresh_token\": \"{{userKey}}\"\r\n}"
    },
    "url": {
      "raw": "https://account.uipath.com/oauth/token",
      "protocol": "https",
      "host": [
        "account",
        "uipath",
        "com"
      ],
      "path": [
        "oauth",
        "token"
      ]
    },
    "description": "**FOR CLOUD ORCHESTRATOR USERS**\n\nThe first step is to send Orchestrator your **_Client ID_** and **_User Key_** so it knows who is making these calls. Orchestrator will return a token that authenticates all subsequent actions.\n\n**Notes:**\n * The URL for this call differs from the rest. It is https://account.uipath.com.\n\n * All calls to the Cloud API require the `X-UIPATH-TenantName` header. Tenant is extracted here from the URL in your environment.\n\n * Don't worry about copying and pasting any tokens once authenticated; this collection does it for you! You can make any call now and it will first ensure that a valid token already exists, retrieving a new token otherwise."
  },
  "response": [
    {
      "name": "Authenticate (Cloud)",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          },
          {
            "key": "User-Agent",
            "value": "",
            "disabled": true
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\r\n    \"grant_type\": \"refresh_token\",\r\n    \"client_id\": \"{{clientId}}\",\r\n    \"refresh_token\": \"{{userKey}}\"\r\n}"
        },
        "url": {
          "raw": "https://account.uipath.com/oauth/token",
          "protocol": "https",
          "host": [
            "account",
            "uipath",
            "com"
          ],
          "path": [
            "oauth",
            "token"
          ]
        }
      },
      "_postman_previewlanguage": null,
      "header": null,
      "cookie": [
      ],
      "body": "{\r\n    \"access_token\": \"abcdefghijklmnopqrstuvwxyz1234567890\",\r\n    \"id_token\": \"z0y9x8w7v6u5t4s3r2q1ponmlkjihgfedcba\",\r\n    \"scope\": \"openid profile email offline_access\",\r\n    \"expires_in\": 86400,\r\n    \"token_type\": \"Bearer\"\r\n}"
    }
  ]
}