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

Adds Bearer Token authentication #202

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions ImportJSON.gs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,35 @@ function ImportJSONBasicAuth(url, username, password, query, parseOptions) {
return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_);
}


/**
* Helper function to authenticate with token based 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
* the JSON feed. The remaining rows contain the data.
*
* The fetchOptions can be used to change how the JSON feed is retrieved. For instance, the "method" and "payload" options can be
* set to pass a POST request with post parameters. For more information on the available parameters, see
* https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app .
*
* Use the include and transformation functions to determine what to include in the import and how to transform the data after it is
* imported.
*
* @param {url} the URL to a http basic auth protected JSON feed
* @param {username} the Username for authentication
* @param {password} the Password for authentication
* @param {query} the query passed to the include function (optional)
* @param {parseOptions} a comma-separated list of options that may alter processing of the data (optional)
*
* @return a two-dimensional array containing the data, with the first row containing headers
* @customfunction
**/
function ImportJSONTokenAuth(url, token, query, parseOptions) {
var header = {headers: {Authorization: "Bearer " + token}};
return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_);
}

/**
* Encodes the given value to use within a URL.
*
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Here are all the functions available:
| **ImportJSONFromSheet** | For use by end users to import JSON from one of the Sheets |
| **ImportJSONViaPost** | For use by end users to import a JSON feed from a URL using POST parameters |
| **ImportJSONBasicAuth** | For use by end users to import a JSON feed from a URL with HTTP Basic Auth |
| **ImportJSONTokenAuth** | For use by end users to import a JSON feed from a URL with Token based Auth |
| **ImportJSONAdvanced** | For use by script developers to easily extend the functionality of this library |

Review `ImportJSON.gs` for more info on how to use these in detail.
Expand Down