PHP 将 Curl 翻译成 Google 应用程序脚本提取 URL

PHP Curl Translation to Google App Script Fetch URL

我有下面的工作 PHP 代码调用 API。 API 将使用一些 header 并将它们加密以与 Client-Signature 进行比较,Client-Signature 也是 header 中的一项。

我翻译成 Google 应用程序脚本。但不幸的是,我收到了 App Script 代码的签名错误。

我的问题是,Google 应用程序脚本是否会以与 PHP 代码相同的方式呈现 header?因为,排除了所有其他问题,我怀疑 headers 在 HTTP 请求中呈现的方式可能会导致签名问题。

如果您看到其他问题,请告诉我。我完全被困在这里。

$requestId = "**";
$tokenID = "**";
$signature = "**";

$ch = curl_init();
$url = "https://public-api.sandbox.bunq.com/v1/user/3079/monetary-account";
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$headers = [
    "Cache-Control: no-cache",
    "Content-Type: application/json",
    "User-Agent: VBA-Web v4.1.5 (https://github.com/VBA-tools/VBA-Web)",
    "X-Bunq-Geolocation: 0 0 0 0 NL",
    "X-Bunq-Language: en_US",
    "X-Bunq-Region: en_US",
    "X-Bunq-Client-Request-Id: " . $requestId,
    "X-Bunq-Client-Authentication: " . $tokenID,    
    "X-Bunq-Client-Signature: " . $signature 
];
var_dump( $headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump ( $server_output) ;

Google 应用程序脚本代码:

  var requestId = "**";
  var url = "https://public-api.sandbox.bunq.com/v1/user/3079/monetary-account";
  var tokenID = "**";
  var signature = "**";
  var RequestHeader =  {
    "Cache-Control" : "no-cache",
    "Content-Type" : "application/json" ,
    "User-Agent" : "VBA-Web v4.1.5 (https://github.com/VBA-tools/VBA-Web)",
    "X-Bunq-Geolocation" : "0 0 0 0 NL",
    "X-Bunq-Language" : "en_US",
    "X-Bunq-Region" : "en_US",
    "X-Bunq-Client-Request-Id" : requestId,
    "X-Bunq-Client-Authentication" : tokenID,   
    "X-Bunq-Client-Signature" : signature 
  };
  var options = {
  "method": "GET",
  "headers": RequestHeader,
  muteHttpExceptions : true
 }
 var response = UrlFetchApp.fetch(url, options);
Logger.log(response );

感谢大家的建议。 创建了一个打印它接收到的 headers 的 Web 服务并发现了这个。

Google 应用程序脚本会放置自己的 User-Agent 值,无论您在 fetch 方法中的 User-Agent 中放置什么。 UrlFetchApp.getRequest 不显示什么 User-Agent 值进入网络服务。它只显示你输入的内容。

所以在这种情况下,我将 User-Agent 的值放在 header 中:

VBA-Web v4.1.5 (https://github.com/VBA-tools/VBA-Web)

getRequest 在 header 中显示 User-Agent 的以下值:

VBA-Web v4.1.5 (https://github.com/VBA-tools/VBA-Web)

当请求到达下面的 Web 服务时,User-Agent 的 header 发生了什么:

Mozilla/5.0 (compatible; Google-Apps-Script)

由于在计算签名时使用了 User-Agent,因此出现了签名错误。