简体   繁体   English

从G Suite管理员SDK删除用户帐户的脚本

[英]Script to delete a user account from G Suite Admin SDK

I am writing a script that fetches a data from a spread sheet and based on every entry entered in the spread sheet, it fetches the data which is basically email addresses and deletes a user's account from the domain. 我正在编写一个脚本,该脚本从电子表格中获取数据,并根据在电子表格中输入的每个条目,获取基本上是电子邮件地址的数据,并从域中删除用户的帐户。

//** Delete the users on submitting the "Submit" button

function onFormSubmit(e) {
  //Logger.log(e);
  //Run this function passing on event "e" when the form is submitted.
  //Object e has form parameters sourced from the sheet attached to the form
  deleteUsers(e);
}

//Logs all the info from the spreadsheet and deletes the user
function deleteUsers() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++) {
    Logger.log('managerEmail: ' + data[i][0]);
    Logger.log('email: ' + data[i][1]);
    Logger.log('trasferToEmail: ' + data[i][3]);

    var managerEmail = data[i][0];
    var email = data[i][1];
    var trasferToEmail = data[i][3];

    var request = {
   'url': 'https://www.googleapis.com/admin/directory/v1/users/' + email,
   'method' : 'DELETE'
    };
  }

But I am still unsuccessful in deleting an account. 但是我仍然无法删除帐户。 I actually tried to implement it based on this doc https://developers.google.com/admin-sdk/directory/v1/reference/users/delete but didn't know how to use it. 我实际上试图根据此文档https://developers.google.com/admin-sdk/directory/v1/reference/users/delete实施它,但不知道如何使用它。 Any ideas? 有任何想法吗? Sorry if this is a stupid question! 抱歉,这是一个愚蠢的问题! I am a novice in Google scripts. 我是Google脚本的新手。

It would be better to use AdminDirectory.Users.remove(email); 最好使用AdminDirectory.Users.remove(email); rather than making a request to the API like you are doing. 而不是像您一样向API发出请求。

Keep it in a variable if you want to log the response var request = AdminDirectory.Users.remove(data[i][1]); 如果要记录响应,请将其保留在变量中var request = AdminDirectory.Users.remove(data[i][1]);

To activate the AdminDirectory; 激活AdminDirectory;

  1. Go to Resources -> Advanced Google services 转到资源->高级Google服务 在此处输入图片说明

  2. Enable the Admin Directory and then click on the link to the Google API console. 启用管理员目录,然后单击指向Google API控制台的链接。 在此处输入图片说明

  3. Click Enable APIs and Services 单击启用API和服务 在此处输入图片说明
  4. Search for Admin SDK 搜索管理SDK 在此处输入图片说明
  5. Click on Admin SDK and then click 'Enable' 点击管理SDK,然后点击“启用”

You're sending e to deleteUsers() , but that function doesn't receive any parameters. 您正在将e发送到deleteUsers() ,但是该函数未接收任何参数。 No need to access the spreadsheet data when it's already provided by the onFormSubmit() –look at the event object documentation for reference. onFormSubmit()已提供电子表格数据时,无需访问该电子表格数据–请查看事件对象文档以供参考。

function deleteUser(e) { 
  var data = e.namedValues;
  var managerEmail = data["Manager Email"][0]; //You'll need to adjust the field names to match with your form data
  var email = data["Email"][0];
  var transferToEmail = data["Transfer to Email"][0];
  var response = AdminDirectory.Users.remove(email);
}

To make sure that your trigger is correctly set up, first have the form responses get saved to your spreadsheet. 为了确保正确设置触发器,请首先将表单响应保存到电子表格中。 Then Edit > Current project's triggers and copy these settings: 然后编辑>当前项目的触发器并复制以下设置: 在此处输入图片说明

To make AdminDirectory work, you need to enable advanced services . 要使AdminDirectory正常工作,您需要启用高级服务 (In the script editor, go to Resources > Advanced Google services and switch "on" Admin Directory API. Then click the link at the bottom of the modal to enable the Admin SDK in the API Console.) (在脚本编辑器中,转到“资源”>“高级Google服务”并“打开” Admin Directory API。然后单击模式底部的链接以在API控制台中启用Admin SDK。) 在此处输入图片说明

If I was mistaken about what data the form is collecting and you really do need to pull the data from spreadsheet (assuming the form isn't connected to the sheet), then you need to create a trigger for when there is a submission to that form. 如果我误认为表单正在收集什么数据,并且确实需要从电子表格中提取数据(假设表单未连接到工作表),那么您需要为何时提交该表单创建触发器 。形成。 Run this function to install the trigger. 运行此功能以安装触发器。

function installFormTrigger() {
  var form = FormApp.openById("FORM_ID");
  ScriptApp.newTrigger("deleteUsers")
  .forForm(form)
  .onFormSubmit()
  .create();
}

Then your original deleteUsers() function will work almost as you had it, but with the addition of AdminDirectory . 然后,原始的deleteUsers()函数几乎可以像您一样运行,但是需要添加AdminDirectory

function deleteUsers() {
  var sheet = SpreadsheetApp.getActive().getSheetByName("SheetName"); //HIGHLY recommend using this instead
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++) {
    var managerEmail = data[i][0];
    var email = data[i][1];
    var trasferToEmail = data[i][3];
    var response = AdminDirectory.Users.remove(email);
  }
}

Note that in your for loop, you might come across an invalid email or AdminDirectory could through an error, so I'd suggest implementing try...catch and logging . 请注意,在您的for循环中,您可能会遇到无效的电子邮件,或者AdminDirectory可能会通过错误进行处理,因此我建议实现try ... catchlogging

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Google 服务帐户和 Google API 根据其用户 ID 验证 G-Suite 用户的密码 - How to use Google Service Account and Google API to validate G-Suite User's Password against its user ID 使用 Google Admin SDK 中的 google app 脚本自动创建用户 - Automate user creation using google app script in Google Admin SDK 获取G Suite中给定用户列表的用户活动报告 - Getting User Activity Reports for a given list of users in G Suite Firebase管理员SDK-删除响应不正确 - Firebase admin SDK - DELETE response not correct 如何从G Suite托管域检索组和成员数据 - How to retrieve groups and members data from G Suite Hosted Domain 如果用户有管理员,不要删除消息? - Don't delete message if user has admin? 用于将G Suite中具有相同名称的PDF文件合并的脚本设置为每周触发 - Script to merge PDF files with same name in G Suite set to weekly trigger 具有多个用户帐户的Google Drive SDK OAuth2 - Google Drive SDK OAuth2 with multiple user account Google Apps Script:如何使用 Google App Script 从单个 GA 帐户创建用户列表并每月一次将其邮寄到 CSV - Google Apps Script: how to create user list from single GA account using Google App Script and mail it in CSV once a month 从个人 twitter 帐户中删除所有推文 - Delete all tweets from personal twitter account
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM