dean2020
dean2020

Reputation: 665

UrlFetchApp.fetch API "Request contains invalid authentication headers" (cURL works)

Making an API request works using PHP with cURL, but I can't get it to work in Google Apps script using UrlFetchApp

working PHP code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: ".$contenttype, "X-BOL-Date:" .$date, "X-BOL-Authorization: ".$signature));
curl_setopt($ch, CURLOPT_URL, $url.$uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if(curl_errno($ch))
{
print_r(curl_errno($ch), true);
}
$result = curl_exec($ch);
curl_close($ch); 

Now the version I created in Google Apps:

var options = 

   { 

     "method": "GET",
     "content-Type": contenttype,
     "X-BOL-Date": date,
     "X-BOL-Authorization": signature,
     "muteHttpExceptions": true,
  };


var response = UrlFetchApp.fetch(url+uri, options);

// Check return code embedded in response.
var rc = response.getResponseCode();
var responseText = response.getContentText();
if (rc !== 200) {
// Log HTTP Error
Logger.log("Response (%s) %s",
         rc,
         responseText );
}
else {
// Successful GET, handle response normally
Logger.log( responseText );
}

Using this throws a "Request contains invalid authentication headers" error. Possible causes according documentation:

"One of the mandatory request headers is missing or date header is not between +/- 15 minutes of the server time in GMT."

The values used in the header (date in GMT time, signature) are identical to the output in PHP. As far as I can tell everything else is also identical.

So apparently UrlFetchApp doesn't send the exact same headers as with cURL.

Upvotes: 0

Views: 630

Answers (1)

Tanaike
Tanaike

Reputation: 201378

If X-BOL-Date and X-BOL-Authorization are required to include in the header, please try a following modification.

From :

var options = { 
    "method": "GET",
    "content-Type": contenttype,
    "X-BOL-Date": date,
    "X-BOL-Authorization": signature,
    "muteHttpExceptions": true,
};

To :

var options = {
    method: "get",
    headers: {
        "Content-Type": contenttype,
        "X-BOL-Date": date,
        "X-BOL-Authorization": signature,
    },
    muteHttpExceptions: true,
};

I don't know whether this works fine, because I cannot run this. So if this didn't work, I'm sorry.

Upvotes: 2

Related Questions