简体   繁体   English

PHP卷曲转换为Google App脚本获取URL

[英]PHP Curl Translation to Google App Script Fetch URL

I have the below working PHP code that calls an API. 我有以下工作的PHP代码调用API。 The API will use some of the headers and encrypt them to compare with the Client-Signature which is also one item in the header. API将使用一些标头并加密它们以与Client-Signature进行比较, Client-Signature也是标头中的一个项目。

I translated to Google App Script. 我翻译成了Google App Script。 But unfortunately I am getting a signature error for the App Script code. 但不幸的是,我收到了App Script代码的签名错误。

My question is, would Google App Script render the headers the same way at the PHP code shown? 我的问题是,Google App Script会在显示的PHP代码中以相同的方式呈现标题吗? Because, all others issues ruled out, I am doubting that the way headers are rendered in the HTTP request may be causing the signature issue. 因为排除了所有其他问题,我怀疑在HTTP请求中呈现标头的方式可能导致签名问题。

If you see an other issue, please let me know. 如果您发现其他问题,请告诉我。 I am totally stuck here. 我完全被困在这里。

$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 App Script Code: Google App脚本代码:

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

Thanks All for your suggestions. 谢谢大家的建议。 Created a Web Service that prints the headers it receives and discovered this. 创建一个Web服务,打印它收到的标头并发现它。

Google App Script puts its own User-Agent value irrespective of what you put at the User-Agent in fetch method. 无论您在fetch方法中使用User-Agent放置什么,Google App Script都会显示自己的User-Agent值。 UrlFetchApp.getRequest doesn't show what User-Agent value goes to the web service. UrlFetchApp.getRequest不显示用户代理值到Web服务的内容。 It only shows what you put in. 它只显示你输入的内容。

So in this case I put below value for User-Agent in header: 所以在这种情况下,我在标题中放置了User-Agent的值:

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

getRequest shows below value for User-Agent in the headers: getRequest在标题中显示User-Agent的值以下:

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

When the request hits the web service below is what went in the header for User-Agent: 当请求到达下面的Web服务时,User-Agent的标题中包含的内容如下:

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

Since User-Agent was used in calculating the signature, I was getting a Signature error. 由于User-Agent用于计算签名,因此我收到了签名错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM