Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Add Basic Auth capability to ImportJSONViaPost and add Bearer Auth request #218

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions ImportJSON.gs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ImportJSONViaPost For use by end users to import a JSON feed from a URL using POST parameters
ImportJSONAdvanced For use by script developers to easily extend the functionality of this library
ImportJSONBasicAuth For use by end users to import a JSON feed from a URL with HTTP Basic Auth (added by Karsten Lettow)
ImportJSONBearerAuth For use by end users to import a JSON feed from a URL with HTTP Bearer Auth

For future enhancements see https://github.com/bradjasper/ImportJSON/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement

Expand Down Expand Up @@ -108,12 +109,22 @@ function ImportJSON(url, query, parseOptions) {
* @param {fetchOptions} a comma-separated list of options used to retrieve the JSON feed from the URL
* @param {query} a comma-separated list of paths to import. Any path starting with one of these paths gets imported.
* @param {parseOptions} a comma-separated list of options that alter processing of the data
* @param {username} username for basic auth requests
* @param {pasword} password for basic auth requests
* @customfunction
*
* @return a two-dimensional array containing the data, with the first row containing headers
**/
function ImportJSONViaPost(url, payload, fetchOptions, query, parseOptions) {
function ImportJSONViaPost(url, payload, fetchOptions, query, parseOptions, username, password) {
var postOptions = parseToObject_(fetchOptions);

var APIKey = Utilities.base64Encode(username + ":" + password);

if (postOptions["headers"] == null) {
postOptions["headers"] = {
'Authorization': `Basic ${APIKey}`
};
}

if (postOptions["method"] == null) {
postOptions["method"] = "POST";
Expand Down Expand Up @@ -224,7 +235,7 @@ function ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeFunc,
}

/**
* Helper function to authenticate with basic auth informations using ImportJSONAdvanced
* Helper functions to authenticate with basic and beaerer auth informations using ImportJSONAdvanced
*
* Imports a JSON feed and returns the results to be inserted into a Google Spreadsheet. The JSON feed is flattened to create
* a two-dimensional array. The first row contains the headers, with each column header indicating the path to that data in
Expand Down Expand Up @@ -252,6 +263,11 @@ function ImportJSONBasicAuth(url, username, password, query, parseOptions) {
return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_);
}

function ImportJSONBearerAuth(url, bearerToken, query, parseOptions) {
var header = {headers: {Authorization: "Bearer " + bearerToken}};
return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_);
}

/**
* Encodes the given value to use within a URL.
*
Expand Down