From 28672cc27f87bfeb024b0d15e1737be9dec66a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Oliveira?= Date: Fri, 8 May 2020 17:15:52 +0200 Subject: [PATCH 1/2] Update ImportJSON.gs Adds ImportJSONTokenAuth method for Token authentication --- ImportJSON.gs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/ImportJSON.gs b/ImportJSON.gs index 731806e..5dfb296 100644 --- a/ImportJSON.gs +++ b/ImportJSON.gs @@ -1,7 +1,7 @@ /*====================================================================================================================================* ImportJSON by Brad Jasper and Trevor Lohrbeer ==================================================================================================================================== - Version: 1.5.0 + Version: 1.6.1 Project Page: https://github.com/bradjasper/ImportJSON Copyright: (c) 2017-2019 by Brad Jasper (c) 2012-2017 by Trevor Lohrbeer @@ -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) + ImportJSONTokenAuth For use by end users to import a JSON feed from a URL with HTTP Token Auth (added by Fábio Oliveira) For future enhancements see https://github.com/bradjasper/ImportJSON/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement @@ -22,7 +23,8 @@ ------------------------------------------------------------------------------------------------------------------------------------ Changelog: - + + 1.6.1 (May 7, 2020) Added Token authentication with ImportJSONTokenAuth 1.6.0 (June 2, 2019) Fixed null values (thanks @gdesmedt1) 1.5.0 (January 11, 2019) Adds ability to include all headers in a fixed order even when no data is present for a given header in some or all rows. 1.4.0 (July 23, 2017) Transfer project to Brad Jasper. Fixed off-by-one array bug. Fixed previous value bug. Added custom annotations. Added ImportJSONFromSheet and ImportJSONBasicAuth. @@ -252,6 +254,33 @@ function ImportJSONBasicAuth(url, username, password, query, parseOptions) { return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_); } +/** + * Helper function to authenticate with token auth information 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 {token} the Token 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: "token " + token}}; + return ImportJSONAdvanced(url, header, query, parseOptions, includeXPath_, defaultTransform_); +} + /** * Encodes the given value to use within a URL. * From 7f7de6c2ac0fc38b88493e15541193044e8db02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Oliveira?= Date: Fri, 8 May 2020 17:17:14 +0200 Subject: [PATCH 2/2] Update README.md Adds reference to new ImportJSONTokenAuth method --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e02e4c8..aefcdec 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,13 @@ 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 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. ## Version +- v1.6.1 (May 7, 2020) Added ImportJSONTokenAuth method for token Authentication - v1.6.0 (June 2, 2019) Fixed null values (thanks @gdesmedt1) - v1.5.0 (January 11, 2019) Adds ability to include all headers in a fixed order even when no data is present for a given header in some or all rows. - v1.4.0 (July 23, 2017) - Project transferred to Brad Jasper. Fixed off-by-one array bug. Fixed previous value bug. Added custom annotations. Added ImportJSONFromSheet and ImportJSONBasicAuth.