传递 API 密钥以导入 Google 表格的 JSON 函数脚本



我正在尝试使用importJSON从WordsAPI导入数据,importJSON是Github的Google Sheets中的自定义Google脚本函数。从不需要身份验证的 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);
}

我已经通读了有关 WordsAPI 站点身份验证的文档,他们建议以下代码片段:

// 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文档,但这对我来说意义不大。有没有可能是ImportJSONAdvanced使用POST,而WordsAPI想要GET?如果是这种情况,我该如何修改代码以使其工作?

感谢您的阅读。任何帮助将不胜感激。


编辑:基于@chuckx评论和其他帮助。我在原始代码的第 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_)
}

它有效。

首先,请注意,文档明确指出不能从电子表格调用ImportJSONAdvanced()(即作为单元格内公式(:

* An advanced version of ImportJSON designed to be easily extended by a script. This version cannot be called from within a 
* spreadsheet.

此外,您在最佳猜测中提供的参数与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)。根据文档,params是一个可以包含headers参数的对象,该参数可用于设置 HTTP 请求的标头。

因此,一种方法是定义您自己的应用程序脚本函数,该函数包装ImportJSONAdvanced()并准备所需的标头。

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')

警告:由于我无法访问WordsAPI,因此这些都没有经过测试。

相关内容

  • 没有找到相关文章

最新更新