How to get an Authorization Code from UIPath - google-apps-script

I am trying to start a robot in my UIpath-orchestrator via Google Appscript.
I have already implemented everything into my script from this documentation:
https://dev.joget.org/community/display/KBv6/Integration+with+UiPath+Robotic+Process+Automation#IntegrationwithUiPathRoboticProcessAutomation-1.GetAccessandIDTokens
But I am actually facing a problem:
Like described in 1.2 of the documentation, I need the authorization code for generating a refresh token. Since I want to write a script to obtain this automatically, the described way with pasting the URL into the browser manually with the code challenge (which btw works fine in my case) is not the way to go for me, as you probably can imagine.
Does anybody have an idea how to achieve this? I would also be fine, if you have a working Postman- or curl-approach - it wouldn’t be a problem to transform this then by myself.
Thank you in advance.

This will be deprecated anyway... :(
Actually what I have to try now is to set up an external application, which I did. I also created an access token like this:
function getAccessToken() {
var data = {
'grant_type': 'client_credentials',
'client_id': '****',
'client_secret': '*****',
'scope': 'OR.Machines'
};
var header = {
'method': 'post',
'payload': data,
};
var response = UrlFetchApp.fetch("https://cloud.uipath.com/identity_/connect/token", header);
Logger.log(response);
var messageContent = response.getContentText();
var result = JSON.parse(messageContent);
var access_token = result['access_token'];
return access_token;
}
Now what I try to do now is to get a process Release Key with whom I can start the job then...
For testing reasons, I tried it with curl:
curl -H "accept: application/json" -H "Content-type: application/json" -H "X-UIPATH-TenantName: [tenantName]" -H "X-UIPATH-OrganizationUnitId: default" --insecure -v https://cloud.uipath.com/[organization]/[TenantName]/odata/Releases?$filter=ProcessKey" -H "Authorization: Bearer [accesstoken]"
Actually what I see in the return is a 400 Bad request...
What have I done wrong?

Related

Google App Scripts - UrlFetchApp.Fetch unable to post

I am trying to have GAS trigger an external POST request to the Adverity Datatap management API to trigger a fetch (https://help.adverity.com/hc/en-us/articles/360009502839-Datastreams-Triggering-a-Fetch) however, the response I'm receiving indicates that a GET request is being sent instead of a POST request. Is there something specific that seems off in the code below?
function triggerFetch() {
var myURL = 'https://YOUR_STACK.datatap.adverity.com/api/datastreams/684/fetch'
var options, thisDate, headers, data;
thisDate = Utilities.formatDate(new Date(), "EST-5", "yyyy-MM-dd") + 'T00:00:00Z';
headers = {
'Authorization':'Token XXXXXXXXXXXXXXXXXXXXXXXXXX'
};
data = {
'start':thisDate
,'end':thisDate.replace('T00','T12')
}
options = {
method:'POST'
,muteHttpExceptions: true
,headers:headers
,'Content-Type':'application/json'
,payload:JSON.stringify(data)
}
var response = UrlFetchApp.fetch(myURL,options);
Logger.log(response);
}
edit: for additional context, this is the working cURL output from postman
curl --location --request POST 'https://YOUR_STACK.datatap.adverity.com/api/datastreams/684/fetch/' \
--header 'Content-Type: application/json' \
--header 'Authorization: Token XXXXXXXXXXXXXXXXXXXXXXXXXX' \
--data-raw '{
"start": "2021-02-16T00:00:00Z",
"end": "2021-02-16T12:00:00Z"
}'
You can try making options contain string with ""(double quotes) instead of ''(single quotes). It should look like this:
options = {
"method" :"POST"
,"muteHttpExceptions": true
,"headers": headers
,"contentType":"application/json"
,"payload":JSON.stringify(data)
}
I was able to figure this out and am posting an answer in case it becomes relevant for anyone else. The datatap API documentation specifies that the URL should end with a trailing "/". Without this last character, a redirect occurs and the http method is reset to GET instead of POST.

Google Apps Script - Connect to external API by using a token received at login

I'm trying to work with an external API and can't make it work after the first step.
The API is for an affiliate marketing platform, 2Performant, and supports only JSON format.
The first step is to login.
In response several headers are received that define the session.
These are: access-token | client | expiry | token-type | uid
In the next request I need to inject mandatory access-token | client | uid headers.
I managed to do the first step but get an error for my second one.
This is the code that I'm using.
function apiconnect() {
var url = "https://api.2performant.com/users/sign_in.json";
var data = {
"user": {
"email": "mail#example.com",
"password": "mypassword"
}
};
var options = {
"method" : "post",
"contentType" : "application/json",
"payload" : JSON.stringify(data)
};
var response = UrlFetchApp.fetch(url,options);
var text = response.getResponseCode();
var token = response.getHeaders()["access-token"];
var client = response.getHeaders()["client"];
var uid = response.getHeaders()["uid"];
Logger.log(text);
Logger.log(JSON.parse(response.getContentText()));
Logger.log(response.getHeaders());
Logger.log(token);
Logger.log(client);
Logger.log(uid);
var urlPrograms = "https://api.2performant.com/affiliate/programs";
var optionsPrograms = {
"contentType" : "application/json",
"headers" : {"access-token": token,
"client" : client,
"uid" : uid,
}
};
Logger.log(UrlFetchApp.fetch(urlPrograms,optionsPrograms));
}
For the first part I can see the correct responses in the logs but I'm getting an error for the second one. The error is 500 (Internal Server Error).
I've noticed that I get a 401 code ("Provided session is not valid anymore either does not exist at all") if I put a random text instead of the correct one in one of the three needed headers (uid, password or token).
By using curl in the terminal, the second step works, but I can't manage to translate this in the Apps Script.
curl -H "Content-Type: application/json" -X POST -d '{"user":{"email":"mail#example.com","password":"mypassword"}}' -i https://api.2performant.com/users/sign_in.json
curl -H "Content-Type: application/json" -H "access-token: 12345" -H "client: 123" -H "uid: mail#example.com" -i https://api.2performant.com/affiliate/programs
It's probably obvious that i'm not experienced in this so hopefully I was clear enough with my description.
If you can point me to the right direction to replicate the curl in apps script that would be amazing.
Thanks!
It seems it was as simple as just adding .json to the second URL.

Google Cloud HTTP function - Why is the function so picky for cURL json format

I could use some help understanding why my Google Cloud Function is so picky about the format that I pass in JSON.
I was following this guide from Google Cloud. The very first example on the page has you create a function exactly like this:
exports.helloHttp = (req, res) => {
res.send(`Hello ${escapeHtml(req.query.name || req.body.name || 'World')}!`);
};
Then they ask you to trigger it via curl:
curl -X POST HTTP_TRIGGER_ENDPOINT -H "Content-Type:application/json" -d '{"name":"Jane"}'
To my surprise I could not get it to work, I have spent a lot of time playing around with params and options until this worked for me:
curl -X POST HTTP_TRIGGER_ENDPOINT -H "Content-Type:application/json" -d {\"name\":\"Jane\"}
I am using a Windows 10 machine and on a terminal that has node(10) installed(In case that matters).
1. What makes me need to remove the single quotes and add the backslashes, does this format have a name or keywords?
2. Can the answer to question 1 be used in a vanilla javascript XMLHttpRequest? As I have not been able to apply my curl modifications successfully.
var xhr = new XMLHttpRequest();
xhr.open("POST", "myFunctionURL");
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(`{\"name\":\"Jane"}`);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log('response: ', JSON.stringify(xhr), ' status: ', xhr.status);
}
}
I have tried passing numerous params into the send including:
var body = {};
body.name = 'Jane';
*passing body into xhr.send*
var body = `{\"name\",\"Jane\"};
*passing body into xhr.send*
It's not Google Cloud Function, it's the Windows Version of CURL command which has an issue with single quotes when posting JSON payload.
Take a look at the following threads which seem to discuss the same issues but in a different context:
https://superuser.com/questions/1016710/how-to-make-a-curl-post-call-in-windows
https://superuser.com/questions/1291352/curl-command-runs-in-linux-but-not-windows-2008

HTTP Header response accept: application/x-pblist

I have some Issue with an API. I wanna fetch data over an curl command wich is working fine. But I can't handle the return type. The default header-accept look like.
-H 'Accept: application/x-bplist'
What is this fpr a return type and how can i convert it in json.
Some other request with no Accept brings back something like this (this is no json!):
{
"data1" = ();
"data2" = {
"value1" = "a...";
};
}
The () stands for an array {}; seems to be an hash, but what for a datatype is this and how to easily convert it in json?
giving an -H "Accept: application/json" will throw an error.
If you have an Idea, thanks
regards
Mat

INVALID_ARGUMENT error by Google speech API

I want to use Google Speech API's asynchronous transcription service. I've uploaded my audio file on Google buckets and I'm trying to use it with the following code (I've removed my key and my filename from the command, everything else is copied).
curl -H "Content-Type: application/json"
--data '{"audio":{"uri":"https://storage.cloud.google.com/<mybucketname>/<filename>"},
"config":{"encoding":"FLAC","sample_rate_hertz":"16000","language_code":"en_US"}}'
https://speech.googleapis.com/v1/speech:longrunningrecognize?key=<mykey>
I get a 400 INVALID_ARGUMENT error telling me "Request contains an invalid argument". What am I doing wrong in my curl request?
Apparently the problem was in my URI. Google has a special way for referencing items in Google buckets, with "gs:" as the prefix.
curl -H "Content-Type: application/json"
--data '{"audio":{"uri":"gs://<mybucketname>/<filename>"},
"config":{"encoding":"FLAC","sample_rate_hertz":"16000","language_code":"en_US"}}'
https://speech.googleapis.com/v1/speech:longrunningrecognize?key=<mykey>
Note that I got a permission error that I could not overcome after I switched to this approach, but I don't think this is related to the way I'm making my curl request anymore.
Should there be a double quotation mark after <filename> ?
Check the sample_rate_Hertz. You may find that is the format for SYNCHRONOUS requests.
ASYNCHRONOUS is sampleRateHertz.
I have just got it to work so revert if you need to while my memory is still fresh!
I was using the Node JS Client and google storage to store the file.
// The audio file's encoding, sample rate in Hertz, and BCP-47 language code
const audio = {
uri: 'gs://sst_bbc/',
};
const config = {
enableWordTimeOffsets: true,
encoding: 'FLAC',
// sampleRateHertz: 16000,
languageCode: 'en-US',
};
const request = {
audio: audio,
config: config,
};
// Detects speech in the audio file
const [operation] = await client.longRunningRecognize(request);
const [response] = await operation.promise();
fs.writeFileSync('ted_talk.json', JSON.stringify(response), 'utf8');
I made a mistake by not referring to the exact object. The error in the console is very ambiguous. After I set the correct path it started to work properly.
const audio = {
uri: 'gs://sst_bbc/ted_talk.flac',
};
if the file location is correct then double check the encoding type.I