Home > Blockchain >  Baselinker Api Call from Google App Script
Baselinker Api Call from Google App Script

Time:04-22

I struggle to connect to third party api (Baselinker Api) from my App Script.

function makeHttpPostRequestWithAppsScript() {
const url = "https://api.baselinker.com/connector.php?method=getOrders";
const response = UrlFetchApp.fetch(url, {
    "method": "POST",
    "headers": {
        "X-BLToken": "xxxx",
        "Content-Type": "application/json"
    },
    "muteHttpExceptions": true,
    "followRedirects": true,
    "validateHttpsCertificates": true,
    "contentType": "application/json",
    "payload": JSON.stringify({"order_id":"5131"})
});

Logger.log("Response code is %s", response.getResponseCode());
Logger.log(response.getContentText());

}

Any idea where am I going wrong? Of Course token is ok. I am getting error like that :

    Informacje  {"status":"ERROR","error_code":"ERROR_UNKNOWN_METHOD","error_message":"An empty or unknown method has been used"}

That is what it should look like in PHP

    <?php
$methodParams = '{
    "date_confirmed_from": 1407341754,
    "get_unconfirmed_orders": false
}';
$apiParams = [
    "method" => "getOrders",
    "parameters" => $methodParams
];

$curl = curl_init("https://api.baselinker.com/connector.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken: xxx"]);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
$response = curl_exec($curl);

Thanks

CodePudding user response:

I believe your goal is as follows.

  • You want to convert the following PHP script to Google Apps Script.

          <?php
      $methodParams = '{
          "date_confirmed_from": 1407341754,
          "get_unconfirmed_orders": false
      }';
      $apiParams = [
          "method" => "getOrders",
          "parameters" => $methodParams
      ];
    
      $curl = curl_init("https://api.baselinker.com/connector.php");
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_HTTPHEADER, ["X-BLToken: xxx"]);
      curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($apiParams));
      $response = curl_exec($curl);
    

When I saw your PHP script, it seems that the data of $methodParams is sent as the form data. So, in this case, how about the following modification?

Modified script:

function makeHttpPostRequestWithAppsScript() {
  const url = "https://api.baselinker.com/connector.php";
  const response = UrlFetchApp.fetch(url, {
    "method": "POST",
    "headers": { "X-BLToken": "xxxx" },
    "muteHttpExceptions": true,
    "payload": {
      "method": "getOrders",
      "parameters": JSON.stringify({ "order_id": "5131" }),
    }
  });
  Logger.log("Response code is %s", response.getResponseCode());
  Logger.log(response.getContentText());
}

Note:

  • When I saw your sample PHP script and your Google Apps Script, in your PHP script, {"date_confirmed_from": 1407341754,"get_unconfirmed_orders": false} is used as the value of parameters. But, in your Google Apps Script, {"order_id":"5131"} is used. If your sample PHP script works fine and when the above modified Google Apps Script didn't work, please test for replacing {"order_id":"5131"} with {"date_confirmed_from": 1407341754,"get_unconfirmed_orders": false} and test it again.

  • I thought that if {"date_confirmed_from": 1407341754,"get_unconfirmed_orders": false} is used to the above Google Apps Script, it seems that the request is the same with your PHP script. So, if an error occurs, please check each value and your token, again.

  • This modified script supposes that your sample PHP script works. Please be careful about this.

Reference:

  • Related