Google Admin Directory API:未授权错误

时间:2018-07-24 16:25:36

标签: google-apps-script google-docs google-admin-sdk google-apps-script-addon

我有一个脚本,希望我们域的普通用户能够运行。我将其作为文档的附件推出,但是当我的用户运行脚本时,他们会收到此错误:

  

GoogleJsonResponseException:未经授权访问此资源/ API。

我相信是由于这些原因。

var user = Session.getActiveUser().getEmail();

var groupList = AdminDirectory.Groups.list({"userKey":user}).groups;

我认为非管理员用户应该可以将AdminDirectory用于list()和其他只读命令(但不能写入)。但是,我能够找到的文档似乎不适合这个问题。

以上各行的上下文:

function onOpen(e) {
  var ui = DocumentApp.getUi();
  ui.createMenu('Sign Document')
    .addItem('Support Staff Signiture', 'getGroups')
    .addToUi();
}

function getGroups() {
  var user = Session.getActiveUser().getEmail();
  try {
    var groupList = AdminDirectory.Groups.list({"userKey":user}).groups;
  }
  catch (e) {
    DocumentApp.getUi().prompt("There was an error! Report this to tech! Error: " + e);
  }
  var doc = DocumentApp.getActiveDocument();
  var docID = doc.getId();
  
  for (var i = 0; i < groupList.length; i++){
    switch (groupList[i].name) {
      case "CAC Staff":
        var source_folder = DriveApp.getFolderById('ID');
        var dest_folder = DriveApp.getFolderById('ID');
        var googleSheet = SpreadsheetApp.openById('ID');
        break;
      case "HS Staff":
        var source_folder = DriveApp.getFolderById('ID');
        var dest_folder = DriveApp.getFolderById('ID');
        var googleSheet = SpreadsheetApp.openById('ID');
        break;
      case "JH Staff":
        var source_folder = DriveApp.getFolderById('ID');
        var dest_folder = DriveApp.getFolderById('ID');
        var googleSheet = SpreadsheetApp.openById('ID');
        break;
      case "JS Staff":
        var source_folder = DriveApp.getFolderById('ID');
        var dest_folder = DriveApp.getFolderById('ID');
        var googleSheet = SpreadsheetApp.openById('ID');
        break;
      case "WS Staff":
        var source_folder = DriveApp.getFolderById('ID');
        var dest_folder = DriveApp.getFolderById('ID');
        var googleSheet = SpreadsheetApp.openById('ID');
        break;
      case "CS Staff":
        var source_folder = DriveApp.getFolderById('ID');
        var dest_folder = DriveApp.getFolderById('ID');
        var googleSheet = SpreadsheetApp.openById('ID');
        break;
    }
    if (googleSheet) {
      try {
        setStatus(user, googleSheet, doc);
        moveFiles(source_folder, dest_folder, docID);
      }
      catch(e) {
        var error = e;
        DocumentApp.getUi().alert('There was an error! Report this error to tech! ' + e);
      }
    }
  }
  var prompt = 'Document sent to your main office!';
  DocumentApp.getUi().alert(prompt);
}

function setStatus(you, ss, doc) {
  var date = new Date();
  var curDate = Utilities.formatDate(new Date(), "CST", "MM/dd/yyyy")
  var thisMonth = date.getMonth() + 1;
  var lastMonth = date.getMonth();
  var docURL = doc.getUrl();
  var body = doc.getBody().editAsText();
  var a = 0;
  do {
    a++;
    var text = body.findText("Month: " + a);
    if (a >= 13) {
      var prompt = 'Error! Report this error to tech! Document Month = null';
      DocumentApp.getUi().alert(prompt);
      return;
    }
  } while (text === null);
  
  var monthString = text.getElement().asText().getText();
  var docMonth = monthString.replace("Month: ", "");
  var a = 0;
  
  do {
    a++
    var text = body.findText("Week: " + a);
  } while (text === null);
  
  var weekString = text.getElement().asText().getText();
  var week = weekString.replace("Week: ", "");
  var thisMonth = date.getMonth() + 1;
  
  //this Month
  if (thisMonth == docMonth) {
    var sheet = ss.getSheetByName('This Month');
    var lastRow = sheet.getLastRow();
    var lastColumn = sheet.getLastColumn();
    var range = sheet.getRange(1,1,lastRow,lastColumn);
    var data = range.getValues();
    for (var i = 0; i < data.length; i++) {
      if (data[i][0] == you) {
        switch (week) {
          case '1':
            data[i][1] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
            break;
          case '2':
            data[i][2] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
            break;
          case '3':
            data[i][3] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
            break;
          case '4':
            data[i][4] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
        }
      }
    }
    range.setValues(data);
  }
  //last Month
  if (thisMonth == (docMonth - 1)) {
    var sheet = ss.getSheetByName('Last Month');
    var lastRow = sheet.getLastRow();
    var lastColumn = sheet.getLastColumn();
    var range = sheet.getRange(1, 1, lastRow, lastColumn);
    var data = range.getValues();
    for (var i = 0; i < data.length; i++) {
      if (data[i][0] == you) {
        switch(week) {
          case '1':
            data[i][1] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
            break;
          case '2':
            data[i][2] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
            break;
          case '3':
            data[i][3] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
            break;
          case '4':
            data[i][4] = "=HYPERLINK("+"\""+docURL+"\","+"\""+curDate+"\""+")";
        }
      }
    }
    range.setValues(data);
  }
  //More than last month
  if ((thisMonth - docMonth) >= 2) {
    DocumentApp.getUi().alert('This Document is too old to sign!');
    return;
  }
}

function moveFiles(source_folder, dest_folder, docID) { 
  var file = DriveApp.getFileById(docID);
  dest_folder.addFile(file);
  source_folder.removeFile(file);
}

0 个答案:

没有答案