Search code examples
javascriptcurlgoogle-apps-scripthighrise

Accessing Highrise Data through API using GAS


As the title says, I am having issues accessing data in Highrise through their API using Google Script. I am new to google script, so some of the functions are not entirely clear to me. I understand that for the actual use of the API, I will be using UrlFetchApp.fetch(url) and that the url I will be using is foocompanyname.highrisehq.com/partofhighriseyouwishtoaccess.xml. From there I am lost however. The API documentation explains the process of authentication using curl (something I have never used before) and I cannot think of analogous functions in Google Script to accomplish this.

Here is the curl example they use to simply gain access

curl -u 605b32dd:X https://example.highrisehq.com/people/1.xml

And here is part of the function I am using that is trying to accomplish the same thing. The whole function is designed to obtain all of the notes from the companies visible to the user whose credentials are being used.

function obtainData(){
  //enter subject-id of admin
  //            |
  //            V
  var userid = "123456789"
  var payload = 
      {
        "action" : "/companies.xml"
      };
  var options = 
      {
        "headers" : { "USER-AGENT" : "[email protected]",
                     "Authorization" : "Basic" + Utilities.base64Encode( "APIkey_from_Highrise_website" + ":" + "Dummy_password" )
                    },
        "method" : "GET",
        "payload" : payload,
        muteHttpExceptions : true
        
      };
  var xmlCompanies = UrlFetchApp.fetch("https://foocompany.highrisehq.com/companies.xml", options).getContentText();
  Logger.log(xmlCompanies);

However, when I run this I receive the error "[HTTP Basic: Access denied. ]" which I assume means that I have not passed on the credentials correctly. Could anyone perhaps tell me what I am doing wrong? After this step I am fairly confident about getting the data, it's the authorization that is getting me.


Solution

  • Try this, note the space after "Basic"

    function obtainData(){
      //enter subject-id of admin
      //            |
      //            V
      var userid = "123456789"
      var payload = 
          {
            "action" : "/companies.xml"
          };
      var options = 
          {
            "headers" : { "USER-AGENT" : "[email protected]",
                         "Authorization" : "Basic " + Utilities.base64Encode( "APIkey_from_Highrise_website" + ":" + "Dummy_password" )
                        },
            "method" : "GET",
            "payload" : payload,
            muteHttpExceptions : true
    
          };
      var xmlCompanies = UrlFetchApp.fetch("https://foocompany.highrisehq.com/companies.xml", options).getContentText();
      Logger.log(xmlCompanies);