I am attempting to import data from WordsAPI using importJSON, a custom Google script function in Google Sheets from Github. It's easy to import information from an API that does not require authentication. However, the WordsAPI requires a key and I can not figure out how to pass the key to the function. My best guess is:
=ImportJSONAdvanced("https://wordsapiv1.p.mashape.com/words/example/examples", "wordsapiv1.p.rapidapi.com", "<MYKEYHERE>", "/examples/", "noInherit,noTruncate,Headers")
But, this results in an error: Bad value (line 220).
The relevant section of the code is below. Go to the ImportJSON.gs at Github for the complete code.
function ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeFunc, transformFunc) {
var jsondata = UrlFetchApp.fetch(url, fetchOptions);
var object = JSON.parse(jsondata.getContentText());
return parseJSONObject_(object, query, parseOptions, includeFunc, transformFunc);
}
I've read through the documentation regarding authentication at the WordsAPI site and they suggest the following code snippet:
// These code snippets use an open-source library. http://unirest.io/nodejs
unirest.get("https://wordsapiv1.p.mashape.com/words/soliloquy")
.header("X-Mashape-Key", "<MYKEYHERE>")
.header("Accept", "application/json")
.end(function (result) {
console.log(result.status, result.headers, result.body);
});
I have also read through the Google Apps Script documentation on Authorization for Google Services, but it doesn't mean much to me. Is it possible the problem is ImportJSONAdvanced uses POST and the WordsAPI wants GET? If that is the case, how can I modify the code to make it work?
Thank you for reading. Any help would be very much appreciated.
Edit: Based on @chuckx comments and additional help. I added the following at line 255 to the original code.
/**
*
* Wrapper for WordsAPI
*
* @param {url} the URL to a http basic auth protected JSON feed
* @param {api_key} the api_key for authentication
* @param {query} always = ""
* @param {parseOptions} a comma-separated list of options that may alter processing of the data (optional)
*/
function ImportJSON_words(url, api_key, query, parseOptions) {
var header = {
headers: {
'X-Mashape-Key': api_key,
'Accept': 'application/json'
}
}
return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_)
}
It works.
api_key
? Thanks