简体   繁体   中英

PHP Curl Translation to Google App Script Fetch URL

I have the below working PHP code that calls an 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.

I translated to Google App Script. But unfortunately I am getting a signature error for the App Script code.

My question is, would Google App Script render the headers the same way at the PHP code shown? 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.

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:

  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.

Google App Script puts its own User-Agent value irrespective of what you put at the User-Agent in fetch method. UrlFetchApp.getRequest doesn't show what User-Agent value goes to the web service. It only shows what you put in.

So in this case I put below value for User-Agent in header:

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

getRequest shows below value for User-Agent in the headers:

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:

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

Since User-Agent was used in calculating the signature, I was getting a Signature error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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