发布Google Workspace插件后OAuth2.0凭证覆盖问题求助
问题原因与修改方案
问题根源
你遇到的多用户凭证互相覆盖问题,核心原因是使用了脚本级全局属性存储:PropertiesService.getScriptProperties()是所有用户共享的存储空间,所有用户的OAuth凭证都会写入同一个位置,后授权的用户自然会覆盖之前用户的凭证数据。
修改方案
将凭证存储方式改为用户级隔离存储,每个用户的凭证会单独保存在自己的专属存储空间,不会互相干扰。
具体代码修改
只需要修改getService_()函数中的setPropertyStore配置行:
原代码:
.setPropertyStore(PropertiesService.getScriptProperties())
修改为:
.setPropertyStore(PropertiesService.getUserProperties())
修改后的完整getService_()函数
/** * Configures the service. */ function getService_() { return OAuth2.createService('Saleforce') // Set the endpoint URLs. .setAuthorizationBaseUrl('https://login.salesforce.com/services/oauth2/authorize') .setTokenUrl('https://login.salesforce.com/services/oauth2/token') // Set the client ID and secret. .setClientId(CLIENT_ID) .setClientSecret(CLIENT_SECRET) // Set the name of the callback function that should be invoked to // complete the OAuth flow. .setCallbackFunction('authCallback') // 改为用户级属性存储,实现用户凭证隔离 .setPropertyStore(PropertiesService.getUserProperties()) // Set the scopes to be requested. //.setScope('chatter_api refresh_token'); }
补充说明
getUserProperties()绑定到当前操作的用户,每个用户的存储数据完全独立- 对应的
reset()函数也只会清除当前用户的授权状态,不会影响其他用户
内容的提问来源于stack exchange,提问作者whytili
相关产品推荐
相关产品推荐

