Google App Scripts - UrlFetchApp.Fetch unable to post - google-apps-script

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.

Related

Error 401 using Metabase API Session Token in Google Apps Script

I coded a script in Google Apps Script to catch some Metabase data and write it on a Google Sheet. When I make the login using my credentials the function work and I receive the Session Token.
But using the token in the script to access my cards the error 401 Unauthenticated is shown.
var headers = {
"method": "get",
"contentType": "application/json",
"X-Metabase-Session": "22222222-cccc-4444-9999-333333333333",
"muteHttpExceptions": true
};
Logger.log(headers);
var response = UrlFetchApp.fetch(url, headers);
I test the url and session token in my local terminal using curl command and it works normally. The Google Apps Script is almost authorized and doesn't inform any error. Does someone have any idea?
When I saw the official document of Example GET request, the sample curl command can be seen as follows.
curl -X GET \
-H "Content-Type: application/json" \
-H "X-Metabase-Session: 38f4939c-ad7f-4cbe-ae54-30946daf8593" \
http://localhost:3000/api/user/current
From this curl command, it seems that X-Metabase-Session is required to be included in the request header. So when your script is modified, it becomes as follows.
Modified script:
var options = {
"method": "get",
"contentType": "application/json",
"headers": { "X-Metabase-Session": "22222222-cccc-4444-9999-333333333333" }, // Modified
"muteHttpExceptions": true
};
var response = UrlFetchApp.fetch(url, options);
Note:
From I test the url and session token in my local terminal using curl command and it works normally., in this case, I thought that when you test the above modified script, it will work.
Reference:
Example GET request

How to get an Authorization Code from UIPath

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?

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