Jake Thomas
Jake Thomas

Reputation: 21

UrlFetchApp Authentication failed Freshbooks

So I am trying to communicate with the freshbooks api by making the sample request detailed on the developers page of freshbooks (http://developers.freshbooks.com/). We are doing token-based authentication as opposed to using OAuth.

I have my code logging the responses to my requests in a spreadsheet. The responses it has been logging are as follows:

<?xml version="1.0" encoding="utf-8"?>
<response xmlns="http://www.freshbooks.com/api/" status="fail">
  <error>Authentication failed.</error>
  <code>20010</code>
</response>

I have been able to authenticate when using a curl command in console, but not when running the script. Below is the code I used. Left out the logging to spreadsheet part and our specific url and authToken:

// Sample function to call into an API using basic HTTP auth
function freshbooksTest () {
  var url = ;
  var authToken = ;

  var unamepass =authToken+":X";
  var digestfull = "Basic "+unamepass;


  var payload = '<request method="system.current"></request>';

  var options =
    {
      "method" : "post",
      "muteHttpExceptions": true,
      "headers" : {"Authorization": digestfull},
      "payload" : payload
    };

    var response = UrlFetchApp.fetch(url, options);
    var xml = response.getContentText();
}

I have checked out threads where people are having similar problems, but the solutions were either not applicable to my situation, or have already been tried. Any suggestions are welcome.

Upvotes: 0

Views: 517

Answers (2)

John McCombs
John McCombs

Reputation: 636

Looks like you need to base 64 encode your auth token. Code should look like this:

function freshbooksTest () {
  var url = ;
  var authToken = ;

  var unamepass = authToken+":X";
  var digestfull = "Basic "+ Utilities.base64Encode(unamepass);


  var payload = '<request method="system.current"></request>';

  var options =
    {
      "method" : "post",
      "muteHttpExceptions": true,
      "headers" : {"Authorization": digestfull},
      "payload" : payload
    };

    var response = UrlFetchApp.fetch(url, options);
    var xml = response.getContentText();
}

Upvotes: 0

Jason Diller
Jason Diller

Reputation: 3368

Not specifically familiar with UrlFetchApp, but if it doesn't do it for you, you'll need to Base64 encode digestfull before you send it in the header.

Upvotes: 0

Related Questions