jsongoogle-apps-scriptuipath-robotuipath-orchestratoruipath-api

Starting a UIPath Job via Google Appscript


I am trying to start a UIPath-Job in the orchestrator via a Google Appscript.

I set up my unattended Robot and it is working fine, if I start the job manually in the orchestrator or via my assistant . But when I try to start it with a script, it fails. Actually I am trying to do what is described in the following documentation in paragraph 6.2 and 6.3:

https://dev.joget.org/community/display/DX7/Integration+with+UiPath+Robotic+Process+Automation (and later on of course 6.4 :) )

Here is my code:

function authenticateForAT() {

    var data1 = {
        'grant_type': 'refresh-token', 'client_id': '$ClientID',
        'refresh_token': '$UserKey',
    };

    var header1 = {
        'method': 'post',
        'contentType': 'application/json',
        'payload': JSON.stringify(data1)
    };

    var response1 = UrlFetchApp.fetch("https://account.uipath.com/oauth/token", header1);
    var messageContent1 = response1.getContentText();
    var result1 = JSON.parse(messageContent1);
    var access_token = result1['access_token'];

    var header2 = {
        'method': 'get',
        'Accept': 'application/json',
        'Authorization': 'Bearer ' + access_token
    };

    var response2 =
        UrlFetchApp.fetch("https://cloud.uipath.com/{Accountlogicalname}/{tenantname}/orchestrator_/odata/Releases?$filter=ProcessKey%20eq%20%27ProcessName%27", header2);

    var messageContent2 = response2.getContentText();

    var result2 = JSON.parse(messageContent2);
}

I get the access token without a problem. The bizarre part is the "fetch" method in function processReleaseKey(). I get this message from logging "response2":

{
    "message": "You are not authenticated!",
    "errorCode": 0,
    "result": null,
    "targetUrl": null,
    "success": false,
    "error": {
        "code": 0,
        "message": "You are not authenticated!",
        "details": "You should be authenticated (sign in) in order to perform this operation.",
        "validationErrors": null
    },
    "unAuthorizedRequest": true,
    "__abp": true
}

Even more bizarre is that I can perform these tasks (and even start the job like in 6.4 of the docu) without any problems in Postman or in the cmd.

I hope you guys can help me.


Solution

  • So the structure was somehow not acceptable. The header on the second request has to look like this: var header2 = { 'method': 'get', headers: { accept: 'application/json', contentType: 'application/json;odata.metadata=minimal;odata.streaming=true', authorization: 'Bearer ' + accessToken } };