Baselinker Api 从 Google 应用脚本调用

Baselinker Api Call from Google App Script

我很难从我的应用程序脚本连接到第三方 api (Baselinker Api)。

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

}

知道我哪里出错了吗?当然令牌还可以。 我收到这样的错误:

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

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

谢谢

我相信你的目标如下。

  • 您想将以下 PHP 脚本转换为 Google Apps 脚本。

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

当我看到你的PHP脚本时,似乎$methodParams的数据是作为表单数据发送的。那么,在这种情况下,下面的修改呢?

修改后的脚本:

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

注:

  • 当我看到你的示例 PHP 脚本和你的 Google Apps 脚本时,在你的 PHP 脚本中,{"date_confirmed_from": 1407341754,"get_unconfirmed_orders": false} 被用作值parameters 个。但是,在您的 Google Apps 脚本中,使用了 {"order_id":"5131"}。如果您的示例 PHP 脚本工作正常,而当上面修改的 Google Apps 脚本不起作用时,请测试将 {"order_id":"5131"} 替换为 {"date_confirmed_from": 1407341754,"get_unconfirmed_orders": false} 并再次测试。

  • 我想,如果{"date_confirmed_from": 1407341754,"get_unconfirmed_orders": false}用于上面的Google Apps Script,看来请求与你的PHP脚本是一样的。因此,如果发生错误,请再次检查每个值和您的令牌。

  • 此修改后的脚本假设您的示例 PHP 脚本有效。请注意这一点。

参考: