0

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.

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124

2 Answers2

0

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.

Jason Diller
  • 3,360
  • 2
  • 24
  • 25
0

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();
}
John McCombs
  • 636
  • 7
  • 6