Authentication Issue using Google App Script

Hi guys,

I am trying to connect to API through G App Script and I am having some problems and getting the following log message: {error=Invalid Authorization, type=ch:service}. Please see the code below:

function myFunction() {

var id = “1298GArBDveRacbw9koz8eluBsZUrACuNNj9_E2U4Ah0”;
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheetByName(“Sheet1”);

var my_api_key = sheet.getRange(1,2,1,1).getValue();

Logger.log(my_api_key);

var headers = {

“Authorization”: “Basic” + Utilities.base64Encode(my_api_key+":"),

}

var params = {
“method”:“GET”,

“headers”:headers,
muteHttpExceptions: true,
};

var url = “https://api.companieshouse.gov.uk/company/00000006”;

var response = JSON.parse(UrlFetchApp.fetch(url, params).getContentText());
Logger.log(response);

}
Any ideas ?

Thank you,
Emanuel

In your example script you seem to be missing a space between “Basic” and your encoded username and password:

"Authorization": "Basic" + Utilities.base64Encode(my_api_key+":"),

… should be

"Authorization": "Basic " + Utilities.base64Encode(my_api_key+":"),

You should be generating something like:

Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l

(For documentation / examples see e.g. Mozilla Dev network).

Hi,

I’ve updated the authorization as instructed but I keep getting the same error message. Any suggestions?

Thank you,
Emanuel

Assuming everything else is correct (e.g. you’ve got a “GET” and the Google Apps ref says “get” but that’s probably not the root cause here) then the thing to do is check all this works using another protocol e.g. make a curl request using your api key from the server you’ve registered with CH (to check that the ip / API key itself is OK). I see you’ve got a log to dump the API key itself so check here that you have what you think e.g. don’t have an extra “:” at the end here etc. There’s a good number of threads here showing people’s working tracking issues in their code in various languages / scripts / packages e.g.:

Thank you for taking your time in helping me…I’ve deleted the initial API key and generated a new one…it is working now.
Updated script, in case anyone else needs this:

function myFunction() {

var id = “SheetId”;
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheetByName(“Sheet1”);

var my_api_key = sheet.getRange(1,2,1,1).getValue();

Logger.log(my_api_key);

var headers = {

“Authorization”: “Basic " + Utilities.base64Encode(my_api_key+”:"),

}

var params = {
“method”:“GET”,

“headers”:headers,
muteHttpExceptions: true,
};

var url = “https://api.companieshouse.gov.uk/company/00000006”;

var response = JSON.parse(UrlFetchApp.fetch(url, params).getContentText());
Logger.log(response);

}

1 Like

Hi guys,

I wanted to know whether this part (/api.company-information.service.gov.uk) of the URL is static or if we need to provide the company’s URL in which we want to search the employee

https://api.company-information.service.gov.uk/search/officers?q=xyz

The documentation’s here:
https://developer-specs.company-information.service.gov.uk/companies-house-public-data-api/reference/search/search-officers

Short answer - no, you don’t. It’s always https://api.company-information.service.gov.uk (well - aside from the Documents API which is https://document-api.company-information.service.gov.uk - but that’s slightly different).

Think about it - if what you suggest is true that would mean that there was a law requiring everyone to link into Companies House API. Which there isn’t. Or that Companies House had hacked every company’s web server!

If what you mean is “can I search for officers AND company combined?” the answer is either “I think not”. Or - if you’re sure about the company “yes - but it’s slightly different”. If you know the company the simplest way is to look up the company first. You can use the Company Search (or the Advanced Search). If you’ve already got the company number that’s all you need so no need to search first. Either follow the company profile link from the search results or (if you already have the number) just call the Company Profile endpoint on the given company. You can then request the Officers of that company and look through their names yourself.