google-apps-script - 权限不足:应用脚本中的请求没有足够的身份验证范围错误,具有适当的范围调用 Directory API
问题描述
我在 Google Sheet 上有一个 App Script 项目。应用脚本功能是使用触发的onFormSubmit
,因此当用户提交 Google 表单时,它会被添加到工作表中,并且理想情况下会发送一封电子邮件。我想检查电子邮件地址并从他们的电子邮件地址中查找用户的姓名,以使消息看起来更好。
但是,每次我尝试调用 Directory API 时,都会收到以下错误:
GoogleJsonResponseException: API call to directory.users.get failed with error: Insufficient Permission: Request had insufficient authentication scopes.
at sendFormByEmail(Code:33:35)
因为我已经在 Advanced Serivces 下启用了 Directory API,所以我创建了一个新.gs
文件进行测试。它工作得很好。在运行时,完全相同的代码行test.gs
失败project.gs
,我不明白为什么。
代码片段:
function sendFormByEmail(e)
{
// Remember to replace this email address with your own email address
var email = "myemail@company.com";
// load spreadsheet where results were added and init default vars
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
var subject = "";
var term_obj =
{
timestamp: 0,
//object creation
};
// The variable e holds all the form values in an array.
term_obj.timestamp = e.namedValues[headers[0]].toString();
term_obj.termed_user = e.namedValues[headers[2]].toString();
//more assignments
// verify the user exists
Logger.log("Termed user is: " + term_obj.termed_user);
Logger.log(AdminDirectory.Users.get(term_obj.termed_user));
当我插入我知道有效的用户的静态字符串时,它也会失败。
https://www.googleapis.com/auth/admin.directory.user.readonly
列在范围内。Admin Directory API
已启用。发生了什么?
解决方案
我发现我的错误 - 我在我的非管理员帐户上创建了触发器。我不得不删除那个表单提交触发器,然后在我的超级管理员帐户上重新创建它。
推荐阅读
- r - 使用 tidyverse 从 R 中的列表中提取项目
- javascript - .style.display = "block" 不知道如何解决这个问题?
- reactjs - 没有得到 Bootstrap 原色
- c# - “素数求和”耗时太长
- javascript - 视频作为纯 css 和 html 中的 div 背景
- python - 想要使用 for 循环打印所有 0 元素的多维列表
- machine-learning - LSTM 序列预测仅在一个特定值上过拟合
- email - 在现有服务器上为现有域配置 IMAP/SMTP (Ubuntu 20.04)
- android - 在 setOnClickListeners (Kotlin) 中,{ it } 和 { view: View -> view...} 有什么区别?
- python - 如何找出另一台计算机连接到的路由器/交换机