The example code was given to me as this.
Sample Call
curl --location --request POST 'https://authentication.carfax.ca/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;
did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' \
--data-urlencode 'audience=https://api.carfax.ca' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=PROVIDEDBYINTEGRATIONSUPPORT' \
--data-urlencode 'client_secret=PROVIDEDBYINTEGRATIONSUPPORT'
My App script code is written as the following where client_id is changed to my actuall client id and client_secret is also updated. I can't get this code to work. Can anyone suggest how i should rewrite the code?
function myFunction() {
var url = 'https://authentication.carfax.ca/oauth/token';
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg',
"data-urlencode": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, headers);
var text = response.getResponseCode();
Logger.log(headers);
}
I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script.
curl --location --request POST 'https://authentication.carfax.ca/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;
did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' \
--data-urlencode 'audience=https://api.carfax.ca' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=PROVIDEDBYINTEGRATIONSUPPORT' \
--data-urlencode 'client_secret=PROVIDEDBYINTEGRATIONSUPPORT'
In this case, please modify as follows.
From:
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg',
"data-urlencode": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, headers);
To:
var options = {
"headers": { 'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' },
"payload": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, options);
Note:
In your curl command, Cookie is used. In this case, the cookie might be required to be updated. If an error occurs, please check this.
If an error like the status code 403 occurs, I'm worried that in this case, Google side might not be able to access to this URL.
Reference:
fetch(url, params)
Related
Here is what the document says:
Use the code parameter value to make the following request to the OAuth token endpoint in the API with the authorization_code grant type:
curl --location --request POST 'https://api.deliverr.com/oauth/v1/token' \\
--header 'Content-Type: application/x-www-form-urlencoded' \\
--data-urlencode 'code={received_code_value}' \\
--data-urlencode 'grant_type=authorization_code'
And I tried to use google app script
make the code like below
function testGetToken(){
var url = "https://api.staging.deliverr.com/oauth/v1/token"
/*const payload = {
'code':'this is code',
'grant-type':'authorization_code'
};*/
var headers = {
"code": "this is code",
"grant_type": "authorization_code",
"Content-Type": "application/x-www-form-urlencoded"
};
const options = {
'method': 'POST',
'header': headers
//'payload': payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
No matter I put code and grant_type to payload or header
They all return same message
Exception: Request failed for https://api.staging.deliverr.com returned code 422.
Truncated server response:
{"code":422,"message":"{"fields":{"request.grant_type":
{"message":"'grant_type' is required"}}}\n
Please refer to Deliverr API documentation...
(use muteHttpExceptions option to examine full response)
What is going on for my code? Is that urlencode problem or something else? How to make it work?
I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script.
curl --location --request POST 'https://api.deliverr.com/oauth/v1/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'code={received_code_value}' \
--data-urlencode 'grant_type=authorization_code'
You have already confirmed that your curl command worked fine.
In this case, how about the following modification?
Modified script:
function testGetToken() {
const url = "https://api.deliverr.com/oauth/v1/token";
const payload = {
'code': '{received_code_value}',
'grant-type': 'authorization_code'
};
const options = { payload };
const response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
}
When payload is used with UrlFetchApp, the POST method is automatically used.
The default content type of the request header is application/x-www-form-urlencoded.
In your curl command, the data is sent as the form.
Note:
I think that the request of the above sample script is the same as your curl command. But, if an error occurs, please confirm your '{received_code_value}' and 'authorization_code' again.
Reference:
fetch(url, params)
I'm currently using Postman to do an API post request from a CRM software called Intercom.
I followed the below documentation to do this:
https://developers.intercom.com/intercom-api-reference/v0/reference/creating-an-export-job
My purpose is to create a script via Google Apps Script to automate the API request.
I need to give the following elements:
Method: Post
URL: https://api.intercom.io/export/content/data
Headers: Authorization : Bearer 123456789, Accept : application/json, Content-Type: application/json
Body: "created_at_after": 1654041600, "created_at_before": 1656547200 (this is the date in Unix Timestamp)
The only parameter that will change is the body ("created_at_after" and "created_at_before"). Everything else will remain the same.
Below is the script I've created, that currently does not work.
Any help on how to fix this would be appreciated. I'm quite a beginner programmer so apologies in advance if the problem is quite obvious.
function exportjob() {
var url = 'https://api.intercom.io/export/content/data';
var options = {
"Method": "post",
"Headers": {
"Authorization": "Bearer 123456789",
"Accept": "application/json",
"Content-Type": "application/json"
},
"Body": {
"created_at_after": 1654041600,
"created_at_before": 1656547200}
}
var response = UrlFetchApp.fetch(url, options);
}
From your showing document, I believe your goal is as follows.
You want to convert the following curl command to Google Apps Script.
curl https://api.intercom.io/export/content/data \
-X POST \
-H 'Authorization:Bearer <Your access token>' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' -d '
{
"created_at_after": 1527811200,
"created_at_before": 1530316800
}'
Modification points:
At params of fetch(url, params) of Class UrlFetchApp, there are no properties of Method, Headers, Body.
In your situation, it seems that it is required to be sent the data as the string. And, by the content type of application/json, the data is parsed at the server side.
When these points are reflected in your script, how about the following modification?
Modified script:
function exportjob() {
var url = 'https://api.intercom.io/export/content/data';
var options = {
"method": "post",
"headers": {
"Authorization": "Bearer 123456789",
"Accept": "application/json",
},
"contentType": "application/json",
"payload": JSON.stringify({
"created_at_after": 1654041600,
"created_at_before": 1656547200
})
}
var response = UrlFetchApp.fetch(url, options);
console.log(response.getContentText())
}
Note:
If an error occurs for the request, please confirm your access token and the data again. And, please provide the error message.
Reference:
fetch(url, params)
I'm trying to access my Home Assistant API and it works fine using curl, but not in Google Apps Script using UrlFetchApp. Using curl works fine:
curl -X GET -H "Authorization: Bearer longFunkyCodeLikeThisiJ9.eyJpc3Mijk5fQ.0Fpw8I" -H "Content-Type: application/json" https://mydevice.duckdns.org:8123/api/states/sensor.my_special_sensor
Google Apps Script returns a 400 Bad Request error.
var HOME_ASSISTANT_TOKEN = "longFunkyCodeLikeThisiJ9.eyJpc3Mijk5fQ.0Fpw8I"
var HOME_ASSISTANT_URL = "https://mydevice.duckdns.org:8123/api/"
function testTemp() {
console.log(getDeviceParameters('sensor.my_special_sensor'));
}
function getDeviceParameters(id) {
var url = HOME_ASSISTANT_URL + "states/" + id;
Logger.log(url);
var bearer = "Bearer " + HOME_ASSISTANT_TOKEN;
var headers = {
"Authorization": bearer,
"Content-Type": 'application/json',
};
var params = {
method: 'get',
headers: headers
};
return UrlFetchApp.fetch(url, params ).getContentText();
}
What am I doing wrong?
It seems Google Apps Script may be blocking port 8123. The script functions by using by instead using your nabu casa url which operates out of port 80.
Just change the HOME_ASSISTANT_URL value to https://[yourspecialcode].ui.nabu.casa/api/
I have google sheet with data that I need to send using WhatsApp, so I have the API of Botmaker. They give a cURL and JSON data but I do not know how to use the cURL and JSON to do the post usen a google scrip function.
This is the cURL:
curl
- X POST
--header 'Content-Type: multipart/form-data'
--header 'Accept: application/json'
--header 'access-token: myAccessToken'
-F chatPlatform=whatsapp
-F chatChannelNumber=############
-F platformContactId=############
-F mediaType=document
This is the Request URL of the API: 'https://go.botmaker.com/api/v1.0/message/binary/v3'
This is the Response Body:
no content
This the Response Code:
401
Response Headers:
{
"accept": "[application/json, application/xml, text/plain]",
"access-control-allow-credentials": "true",
"access-control-allow-headers": "bearer-token,access-token,Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin",
"access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS, HEAD",
"access-control-allow-origin": "https://go.botmaker.com",
"alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
"cache-control": "must-revalidate,no-cache,no-store",
"content-length": "0",
"content-security-policy": "default-src 'self'",
"date": "Sat, 11 Jun 2022 22:52:02 GMT",
"permissions-policy": "geolocation=(self \"https://go.botmaker.com\"), microphone=()",
"reason-phrase": "Cannot authenticate user with received tokens after applying [access-token] and accessToken [null]",
"referrer-policy": "no-referrer",
"server": "Botmaker",
"strict-transport-security": "max-age=31536000; includeSubDomains",
"via": "1.1 google",
"x-content-type-options": "nosniff",
"x-frame-options": "DENY"
}
I understand that I need to use the UrlFetchApp.fetch
Someone can help me to create
I found the way, here is how it works for me:
var userNumber = '123456789012' // to: cellular phone number
const waNumber = '123456789012'; // from: cellular phone
const accessToken = 'faketoken'; // use you token
// send a template
var url = 'https://go.botmaker.com/api/v1.0/intent/v2';
var headers = {
'Content-Type': 'application/json' ,
'Accept': 'application/json' ,
'access-token': accessToken
};
var dataJson = {
"chatPlatform": "whatsapp",
"chatChannelNumber": waNumber,
"platformContactId": userNumber,
"ruleNameOrId": "template_name" // the name of the template
};
var payload = JSON.stringify(dataJson);
var options = {
'method': 'POST',
'headers': headers,
'payload': payload
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response);
I always get:
Missing end boundary in multipart body.
API Sandbox doesn't help. It is possible to create folder there.
The request is:
curl https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart -d #/home/payload -H 'Authorization: Bearer mytoken' -H 'Content-Type: multipart/boundary; boundary=RubyApiClientUpload'
The payload is:
--RubyApiClientUpload
Content-Type: application/json
{ "mimeType":"application/vnd.google-apps.folder", "name":"newfolder", "parents":["0AMtAREF....."] }
--RubyApiClientUpload
--RubyApiClientUpload--
You're using the wrong API call. A folder has no content, so you create it by POSTing to the URL in https://developers.google.com/drive/v3/reference/files/create
You can easily test it at https://developers.google.com/drive/v3/reference/files/create#try-it where you should see
POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}
{
"mimeType": "application/vnd.google-apps.folder",
"name": "test folder"
}
This code works for me.
const body = JSON.stringify({
name: "Super folder!!!",
mimeType: "application/vnd.google-apps.folder"
});
const headers = new Headers();
headers.append("Authorization", `Bearer ${accessToken}`);
headers.append("Content-Type", "application/json");
headers.append("Content-Length", body.length);
fetch(
"https://www.googleapis.com/drive/v3/files", {
method: "POST",
headers,
body
});