首页 > 解决方案 > 权限不足:应用脚本中的请求没有足够的身份验证范围错误,具有适当的范围调用 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已启用。发生了什么?

标签: google-apps-script

解决方案


我发现我的错误 - 我在我的非管理员帐户上创建了触发器。我不得不删除那个表单提交触发器,然后在我的超级管理员帐户上重新创建它。


推荐阅读