-2

Currently working on fetching data from an API with Basic Authentication. Through this script: https://gist.github.com/anemitz/8cc6ef6c41ea4cefe51a13fbd6d8676a

function ImportJSONBasicAuthentication(url, query, parseOptions, username, password) {

  var fetchOptions = {
    "headers" : {
       "Authorization" : 'Basic ' + Utilities.base64Encode(username + ':' + password)
    },
    muteHttpExceptions: true
  };

  return ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeXPath_, defaultTransform_);
}

Inserted the script to Google Script Editor (in Google Sheets) but when running the script the same error keeps coming back:

'ReferenceError: "ImportJSONAdvanced" is not defined. (line 10)'

In the actual sheet I am calling

=ImportJSONBasicAuthentication("url", "query", "noInherit, noTruncate", "api_key", )

Am I overlooking something here?

1

2 Answers 2

2

The function ImportJSONBasicAuthentication is basically just a wrapper function for ImportJSONAdvanced but changes some options before calling it (the line with var fetchOptions initiliaztion call sets up options accordingly). You'd need to include the function ImportJSONAdvanced and any functions it might also call (dependancies).

Notice the return value of the ImportJSONBasicAuthentication is just a call to ImportJSONAdvanced. Hence, your error that mentions ImportJSONAdvanced.

So you're just overlooking it's dependent on another function, namely ImportJSONAdvanced.

1

I believe that's relying on the the JSON import library by Chris Keller, found here.

As its description says,

To use go to Tools --> Script Editor and add the script and save.

This means the entire script, not just one function from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.