google-apps-script - OAuth2 库产生属性存储配额错误
问题描述
我有一个使用Google 的 OAuth2 库的脚本,它最近开始因错误而失败
您已超出属性存储配额。请删除一些属性,然后重试。
我检查了属性,并没有发现与原始配置有任何意外差异。我的属性的完整字符串化文本长度约为 5000 个字符,远低于500kB/属性存储配额和最长的单个属性约 2500 个字符(低于 9kB/值配额)。
我发现仅当我将Google 的 OAuth2 库与服务帐户一起使用时才会发生此错误。但是,如果我将库 dist直接包含在我的项目中,错误就会消失。
如果库和本地副本看起来是相同的版本,为什么属性服务的行为方式会有所不同?
(在我使用带有标准授权代码授予流程的 OAuth2 库的其他脚本中,我没有问题。)
此脚本将复制错误,但要求您创建服务帐户并设置正确的脚本属性,如getAdminDirectory_()
. (使用 Rhino 解释器。)
/**
* Get a GSuite User.
*/
function getUser() {
var email = "name@exampledomain.com";
var user = AdminDirectory_getUser_(email);
Logger.log(user);
}
/**
* Gets a user from the GSuite organization by their email address.
* @returns {User} - https://developers.google.com/admin-sdk/directory/v1/reference/users#resource
*/
function AdminDirectory_getUser_(email) {
var service = getAdminDirectory_();
if (service.hasAccess()) {
var url = "https://www.googleapis.com/admin/directory/v1/users/" + email;
var options = {
method: "get",
headers: {
Authorization: "Bearer " + service.getAccessToken()
}
};
var response = UrlFetchApp.fetch(url, options);
var result = JSON.parse(response.getContentText());
return result;
} else {
throw service.getLastError();
}
}
/**
* Configures the service.
*/
function getAdminDirectory_() {
// Get service account from properties
var scriptProperties = PropertiesService.getScriptProperties();
var serviceAccount = JSON.parse(scriptProperties.getProperty("service_account"));
// Email address of the user to impersonate.
var user_email = scriptProperties.getProperty("service_account_user");
return OAuth2.createService("AdminDirectory:" + user_email)
// Set the endpoint URL.
.setTokenUrl("https://oauth2.googleapis.com/token")
// Set the private key and issuer.
.setPrivateKey(serviceAccount.private_key)
.setIssuer(serviceAccount.client_email)
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
.setSubject(user_email)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(scriptProperties)
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope("https://www.googleapis.com/auth/admin.directory.user");
}
解决方案
这似乎是一个已知问题。考虑在此问题上添加星号(左上角), 让 Google 开发人员知道您受到了影响。考虑向跟踪器添加评论,其中包含从#7请求的详细信息
可能的解决方案:
如问题中所述,通过复制粘贴直接使用库代码,而不是使用错误的库功能。
切换到 v8- #21
等待随机/自动分辨率 - #14
推荐阅读
- reactjs - 路由不是在构建 React js Web 应用程序之后
- mysql - 无法使用 IF ... THEN 语句执行查询
- java - 当从 hashmap.values() 初始化的列表中删除一个项目时会发生什么?
- cron - zfs-auto-snapshot 的 cron 作业中双斜线的目的是什么?
- python - Mayavi:自定义工具栏
- javascript - 三.JS:根据不同的tileset渲染一张大图(Texture Atlas)
- r - ARSnova 系统的闪存卡
- selenium - 如何根据 Selenium 提供的 HTML 定位输入标签?
- c# - 当前缀有效时,如何防止匈牙利符号的 StyleCop 警告
- python - 我们如何在运行时将最大 CPU 使用率分配给 python 代码?