通过 SpreadsheetApp 全局,使用绑定到电子表格的 Apps 脚本来影响电子表格非常简单。但是,有一些功能(例如在工作表上获取/设置过滤器)只能通过 Google Sheets REST API 访问。
我见过一个在 Apps 脚本中使用 UrlFetchApp 来调用 Google Sheet API 的示例,但它的编写方式就好像 Apps 脚本实际上并未绑定到特定电子表格一样。
function getSheetBasicFilter(ssId, sheetId) {
var params = {
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken()
}
};
var base = "https://sheets.googleapis.com/v4/spreadsheets/";
var partialRespParams = "?fields=sheets%2FbasicFilter";
var resp = UrlFetchApp.fetch(base + ssId + partialRespParams, params);
var sheets = JSON.parse(resp.getContentText()).sheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].basicFilter
&& (sheets[i].basicFilter.range.sheetId == sheetId
|| (!sheets[i].basicFilter.range.sheetId && sheetId == 0))) {
return sheets[i].basicFilter;
}
}
return null;
}
当我尝试从电子表格绑定应用程序脚本中调用此函数时,收到错误“请求的身份验证范围不足”。
由于脚本以拥有文档的用户身份运行,并且正在使用 ScriptApp.getOAuthToken(),因此尚不清楚为什么会发生此错误。
任何帮助,将不胜感激。
首先,请确保您启用表格 API在您的开发者控制台中。
错误 ”Request had insufficient authentication scopes
" 是请求中提供的 OAuth 2.0 令牌中的错误,指定的范围不足以访问所请求的数据。
确保您使用正确且所有必需的scopes https://developers.google.com/sheets/guides/authorizing#OAuth2Authorizing,并检查这个使用 OAuth 2.0 授权请求 https://developers.google.com/sheets/guides/authorizing#AboutAuthorization如果您正确遵循此处的步骤。
您也可以尝试撤销访问权限并尝试重做。
有关更多信息,请查看文档ScriptApp.getOAuthToken() https://developers.google.com/apps-script/reference/script/script-app#getOAuthToken()和这个thread https://stackoverflow.com/questions/34573295/google-apps-script-urlfetchapp-returns-404-when-the-url-exists
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)