Saturday, April 23, 2022

[SOLVED] How do I make a CURL call using my app script codes

Issue

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);
}

Solution

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:



Answered By - Tanaike
Answer Checked By - Pedro (WPSolving Volunteer)