Chilkat Online Tools

Ruby / Orchestrator API Documentation / Authenticate (Cloud)

Back to Collection Items

require 'chilkat'

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

http = Chilkat::CkHttp.new()

# 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}}"
# }

json = Chilkat::CkJsonObject.new()
json.UpdateString("grant_type","refresh_token")
json.UpdateString("client_id","{{clientId}}")
json.UpdateString("refresh_token","{{userKey}}")

http.SetRequestHeader("Content-Type","application/json")

# resp is a CkHttpResponse
resp = http.PostJson3("https://account.uipath.com/oauth/token","application/json",json)
if (http.get_LastMethodSuccess() == false)
    print http.lastErrorText() + "\n";
    exit
end

sbResponseBody = Chilkat::CkStringBuilder.new()
resp.GetBodySb(sbResponseBody)

jResp = Chilkat::CkJsonObject.new()
jResp.LoadSb(sbResponseBody)
jResp.put_EmitCompact(false)

print "Response Body:" + "\n";
print jResp.emit() + "\n";

respStatusCode = resp.get_StatusCode()
print "Response Status Code = " + respStatusCode.to_s() + "\n";
if (respStatusCode >= 400)
    print "Response Header:" + "\n";
    print resp.header() + "\n";
    print "Failed." + "\n";

    exit
end

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

# {
#   "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI3IkpXVCIsImtpZCI6IlJUTkVOMEl5T1RWQk1UZEVRVEEzUlRZNE16UkJPVU00UVRRM016TXlSalUzUmpnMk4wSTBPQSJ9.eyJodHRwczovL3VpcGF0aC9lbWFpbCI6InJvYmVydC23YWduZXJAdWlwYXRoLmNvbSIsImh0dHBzOi8vdWlwYXRoL2VtYWlsX3ZlcmlmaWVkIjp0cnVlLCJpc3MiOiJodHRwczovL2FjY291bnQudWlwYXRoLmNvbS8iLCJzdWIiOiJhdXRoMHw1ZmZkNzhkOGNhNzk3NzAwN2JmZWEyMjYiLCJhdWQiOlsi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bWUtUGFzc3dvcmQtQXV0aGVudGljYXRpb24iLCJuaWNrbmFtZSI6InJvYmVydC53YWduZXIiLCJuYW1lIjoicm9iZXJ0LndhZ25lckB1aXBhdGguY29tIiwicGljdHVyZSI1Imh0dHBzOi8vcy5ncmF2YXRhci5jb20vYXZhdGFyLzc5ZWI3OTM1ZDBjZDczYzZlNWVmMThiNTM5Y2JmY2M0P3M9NDgwJnI9cGcmZD1odHRwcyUzQSUyRiUyRmNkbi5hdXRoMC5jb20lMkZhdmF0YXJzJTJGcm8ucG5nIiwidXBkYXRlZF9hdCI6IjIwMjEtMDMtMzFUMTI6MjA6MTYuOTM2WiIsImVtYWlsIjoicm9iZXJ0LndhZ25lckB1aXBhdGguY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzcyI6Imh0dHBzOi8vYWNjb3VudC51aXBhdGguY29tLyIsInN1YiI6ImF1dGgwfDVmZmQ3OGQ4Y2E3OTc3MDA3YmZlYTIyNiIsImF1ZCI6IjhERXYxQU1OWGN6VzN5NFUxNUxMM2pZZjYyaks5M241IiwiaWF0IjoxNjE3MTk1MTg4LCJleHAiOjE2MTcyMzExODh9.F4GrdXgZyPHrK9WQGVjIQx2nPx6tibbZK-SRRIhM85sCAy3F6FDF9ip_dWxR3t9XSVAa-kCrapzOitTnDgW_USCKTt_9ICZOOvq1UTvuXx0uFCjxYM78x3dZPpyE_LUNaBtlEc7c67xrgbPswA6fdyueehsEzJVdt_c9CQSBOflq8vRrElmr5Sk7qsf3RNYQ1u5vG12S-jtjJCnJKBxijK69SPENRVoiFxSBTneS28m53knMbTkhx48a4aTmuhl4QeLlWrFqzfJ7x8iE1TrNXCyaF5mr57s1wXlLA08y8jwHTxy0bQ0EYCYowtFr0iGnNKTZaRcuiCOp4K5wpi2VsZ",
#   "scope": "openid profile email offline_access",
#   "expires_in": 86400,
#   "token_type": "Bearer"
# }

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

access_token = jResp.stringOf("access_token")
id_token = jResp.stringOf("id_token")
scope = jResp.stringOf("scope")
expires_in = jResp.IntOf("expires_in")
token_type = jResp.stringOf("token_type")

Curl Command

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

Postman Collection Item JSON

{
  "name": "Authenticate (Cloud)",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "//test status code\r",
          "pm.test(\"Status code is 200\", function () {\r",
          "    pm.response.to.have.status(200);\r",
          "});\r",
          "\r",
          "//set access token\r",
          "var jsonData = JSON.parse(responseBody);\r",
          "pm.environment.set(\"token\", jsonData.access_token);\r",
          "console.log(pm.environment.get(\"token\"));\r",
          "\r",
          "//check if access token is available\r",
          "pm.test(\"Property Access Token available\", function () {\r",
          "    pm.expect(pm.response.text()).to.include(\"access_token\");\r",
          "});"
        ],
        "type": "text/javascript"
      }
    },
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "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\nHTTP Header: `Authorization`: Bearer {token}\n\nRead how to get your **_Client ID_** and **_User Key_** \n[ here](https://docs.uipath.com/orchestrator/reference/consuming-cloud-api)."
  },
  "response": [
    {
      "name": "Authenticate (Cloud)",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "value": "application/json",
            "type": "text"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\r\n    \"grant_type\": \"refresh_token\",\r\n    \"client_id\": \"{{clientId}}\",\r\n    \"refresh_token\": \"{{userKey}}\"\r\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://account.uipath.com/oauth/token",
          "protocol": "https",
          "host": [
            "account",
            "uipath",
            "com"
          ],
          "path": [
            "oauth",
            "token"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Wed, 31 Mar 2021 12:53:08 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "CF-Ray",
          "value": "6389b803ca690d5a-VIE"
        },
        {
          "key": "Cache-Control",
          "value": "no-store"
        },
        {
          "key": "Strict-Transport-Security",
          "value": "max-age=31536000"
        },
        {
          "key": "Vary",
          "value": "Accept-Encoding, Origin"
        },
        {
          "key": "CF-Cache-Status",
          "value": "DYNAMIC"
        },
        {
          "key": "cf-request-id",
          "value": "0929f1566200000d5a19998000000001"
        },
        {
          "key": "Expect-CT",
          "value": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
        },
        {
          "key": "ot-baggage-auth0-request-id",
          "value": "6389b803ca690d5a"
        },
        {
          "key": "ot-tracer-sampled",
          "value": "true"
        },
        {
          "key": "ot-tracer-spanid",
          "value": "5291a51954262f0f"
        },
        {
          "key": "ot-tracer-traceid",
          "value": "5ee3eb847f96f83f"
        },
        {
          "key": "Pragma",
          "value": "no-cache"
        },
        {
          "key": "X-Auth0-RequestId",
          "value": "f21fde3857e0375b0bfe"
        },
        {
          "key": "X-Content-Type-Options",
          "value": "nosniff"
        },
        {
          "key": "X-RateLimit-Limit",
          "value": "1000000"
        },
        {
          "key": "X-RateLimit-Remaining",
          "value": "999999"
        },
        {
          "key": "X-RateLimit-Reset",
          "value": "1617195188"
        },
        {
          "key": "Server",
          "value": "cloudflare"
        },
        {
          "key": "Content-Encoding",
          "value": "br"
        },
        {
          "key": "alt-svc",
          "value": "h3-27=\":443\"; ma=86400, h3-28=\":443\"; ma=86400, h3-29=\":443\"; ma=86400"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"access_token\": \"eyJhbGciOiJSUzI1NiIsInR5cCI3IkpXVCIsImtpZCI6IlJUTkVOMEl5T1RWQk1UZEVRVEEzUlRZNE16UkJPVU00UVRRM016TXlSalUzUmpnMk4wSTBPQSJ9.eyJodHRwczovL3VpcGF0aC9lbWFpbCI6InJvYmVydC23YWduZXJAdWlwYXRoLmNvbSIsImh0dHBzOi8vdWlwYXRoL2VtYWlsX3ZlcmlmaWVkIjp0cnVlLCJpc3MiOiJodHRwczovL2FjY291bnQudWlwYXRoLmNvbS8iLCJzdWIiOiJhdXRoMHw1ZmZkNzhkOGNhNzk3NzAwN2JmZWEyMjYiLCJhdWQiOlsiaHR0cHM6Ly9vcmNoZXN0cmF0b3IuY2xvdWQudWlwYXRoLmNvbSIsImh0dHBzOi8vdWlwYXRoLmV1LmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE2MTcxOTUxODgsImV4cCI6MTYxNzI4MTU4OCwiYXpwIjoiOERFdjFBTU5YY3pXM3k0VTE1TEwzallmNjJqSzkzbjUiLCJzY29wZSI6Im1wZW5pZCBwcm9maWxlIGVtYWlsIG9mZmxpbmVfYWNjZXNzIn0.HyycgYbs9f6EXyDRZm8w-d0A82bZqBoH594ZN-ZHujMuuoLgRF1cIq8jiAGI83P2nT4hclbVxFWiw8huPqYlu7ps9uXiiV7uKFHY2cbdEyRND1-MRYqL4HyilImyJFuIREwYyl1NHshKWZ1GYepIkX6plKT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nRydWUsImlzcyI6Imh0dHBzOi8vYWNjb3VudC51aXBhdGguY29tLyIsInN1YiI6ImF1dGgwfDVmZmQ3OGQ4Y2E3OTc3MDA3YmZlYTIyNiIsImF1ZCI6IjhERXYxQU1OWGN6VzN5NFUxNUxMM2pZZjYyaks5M241IiwiaWF0IjoxNjE3MTk1MTg4LCJleHAiOjE2MTcyMzExODh9.F4GrdXgZyPHrK9WQGVjIQx2nPx6tibbZK-SRRIhM85sCAy3F6FDF9ip_dWxR3t9XSVAa-kCrapzOitTnDgW_USCKTt_9ICZOOvq1UTvuXx0uFCjxYM78x3dZPpyE_LUNaBtlEc7c67xrgbPswA6fdyueehsEzJVdt_c9CQSBOflq8vRrElmr5Sk7qsf3RNYQ1u5vG12S-jtjJCnJKBxijK69SPENRVoiFxSBTneS28m53knMbTkhx48a4aTmuhl4QeLlWrFqzfJ7x8iE1TrNXCyaF5mr57s1wXlLA08y8jwHTxy0bQ0EYCYowtFr0iGnNKTZaRcuiCOp4K5wpi2VsZ\",\n    \"scope\": \"openid profile email offline_access\",\n    \"expires_in\": 86400,\n    \"token_type\": \"Bearer\"\n}"
    }
  ]
}