将 API 键传递给 Google 工作表的 importJSON 函数脚本
Passing API Key to importJSON Function Script for Google Sheets
我正在尝试从 WordsAPI using importJSON 导入数据,Google 表格中的自定义 Google 脚本函数来自 Github。从不需要身份验证的 API 导入信息很容易。但是,WordsAPI 需要一个键,我不知道如何将键传递给函数。我最好的猜测是:
=ImportJSONAdvanced("https://wordsapiv1.p.mashape.com/words/example/examples", "wordsapiv1.p.rapidapi.com", "<MYKEYHERE>", "/examples/", "noInherit,noTruncate,Headers")
但是,这会导致错误:Bad value (line 220).
代码的相关部分如下。转到 Github 的 ImportJSON.gs 获取完整代码。
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);
}
我通读了 documentation regarding authentication at the WordsAPI site,他们建议使用以下代码片段:
// 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);
});
我也通读了 Google Apps Script documentation on Authorization for Google Services,但这对我来说意义不大。问题是否可能是 ImportJSONAdvanced 使用 POST 而 WordsAPI 想要 GET?如果是这样,我该如何修改代码才能使其正常工作?
感谢您的阅读。任何帮助将不胜感激。
编辑:基于@chuckx 评论和其他帮助。我在 original code 的第 255 行添加了以下内容。
/**
*
* 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_)
}
有效。
首先,请注意 the documentation explicitly states ImportJSONAdvanced()
不能从电子表格调用(即作为 in-cell 公式):
* An advanced version of ImportJSON designed to be easily extended by a script. This version cannot be called from within a
* spreadsheet.
此外,您在最佳猜测中提供的论据与 the argument documentation for ImportJSONAdvanced()
不一致。供参考:
* @param {url} the URL to a public JSON feed
* @param {fetchOptions} an object whose properties are options used to retrieve the JSON feed from the URL
* @param {query} the query passed to the include function
* @param {parseOptions} a comma-separated list of options that may alter processing of the data
* @param {includeFunc} a function with the signature func(query, path, options) that returns true if the data element at the given path
* should be included or false otherwise.
* @param {transformFunc} a function with the signature func(data, row, column, options) where data is a 2-dimensional array of the data
* and row & column are the current row and column being processed. Any return value is ignored. Note that row 0
* contains the headers for the data, so test for row==0 to process headers only.
有趣的是 fetchOptions
参数,它作为 params
参数传递给 UrlFetchAPP.fetch(url, params)
。根据the documentation,params
是一个object,其中可以包含一个headers
参数,可用于设置HTTP请求的header。
因此,一种方法是定义您自己的 Apps 脚本函数,它包装 ImportJSONAdvanced()
并准备所需的 header。
function ImportJSON_WordsAPI(path, query, parseOptions) {
var url = 'https://wordsapiv1.p.mashape.com/words' + path;
var fetchOptions = {
'headers': {
'X-Mashape-Key': '<MYKEYHERE>',
'Accept': 'application/json',
},
}
return ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeXPath_, defaultTransform_);
}
然后,像这样在单元格中使用函数:
=ImportJSON_WordsAPI('/words/soliloquy', '/results', 'noInherit,noTruncate')
警告:none 由于我无法访问 WordsAPI,因此已经过测试。
我正在尝试从 WordsAPI using importJSON 导入数据,Google 表格中的自定义 Google 脚本函数来自 Github。从不需要身份验证的 API 导入信息很容易。但是,WordsAPI 需要一个键,我不知道如何将键传递给函数。我最好的猜测是:
=ImportJSONAdvanced("https://wordsapiv1.p.mashape.com/words/example/examples", "wordsapiv1.p.rapidapi.com", "<MYKEYHERE>", "/examples/", "noInherit,noTruncate,Headers")
但是,这会导致错误:Bad value (line 220).
代码的相关部分如下。转到 Github 的 ImportJSON.gs 获取完整代码。
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);
}
我通读了 documentation regarding authentication at the WordsAPI site,他们建议使用以下代码片段:
// 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);
});
我也通读了 Google Apps Script documentation on Authorization for Google Services,但这对我来说意义不大。问题是否可能是 ImportJSONAdvanced 使用 POST 而 WordsAPI 想要 GET?如果是这样,我该如何修改代码才能使其正常工作?
感谢您的阅读。任何帮助将不胜感激。
编辑:基于@chuckx 评论和其他帮助。我在 original code 的第 255 行添加了以下内容。
/**
*
* 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_)
}
有效。
首先,请注意 the documentation explicitly states ImportJSONAdvanced()
不能从电子表格调用(即作为 in-cell 公式):
* An advanced version of ImportJSON designed to be easily extended by a script. This version cannot be called from within a
* spreadsheet.
此外,您在最佳猜测中提供的论据与 the argument documentation for ImportJSONAdvanced()
不一致。供参考:
* @param {url} the URL to a public JSON feed
* @param {fetchOptions} an object whose properties are options used to retrieve the JSON feed from the URL
* @param {query} the query passed to the include function
* @param {parseOptions} a comma-separated list of options that may alter processing of the data
* @param {includeFunc} a function with the signature func(query, path, options) that returns true if the data element at the given path
* should be included or false otherwise.
* @param {transformFunc} a function with the signature func(data, row, column, options) where data is a 2-dimensional array of the data
* and row & column are the current row and column being processed. Any return value is ignored. Note that row 0
* contains the headers for the data, so test for row==0 to process headers only.
有趣的是 fetchOptions
参数,它作为 params
参数传递给 UrlFetchAPP.fetch(url, params)
。根据the documentation,params
是一个object,其中可以包含一个headers
参数,可用于设置HTTP请求的header。
因此,一种方法是定义您自己的 Apps 脚本函数,它包装 ImportJSONAdvanced()
并准备所需的 header。
function ImportJSON_WordsAPI(path, query, parseOptions) {
var url = 'https://wordsapiv1.p.mashape.com/words' + path;
var fetchOptions = {
'headers': {
'X-Mashape-Key': '<MYKEYHERE>',
'Accept': 'application/json',
},
}
return ImportJSONAdvanced(url, fetchOptions, query, parseOptions, includeXPath_, defaultTransform_);
}
然后,像这样在单元格中使用函数:
=ImportJSON_WordsAPI('/words/soliloquy', '/results', 'noInherit,noTruncate')
警告:none 由于我无法访问 WordsAPI,因此已经过测试。