שירות Directory SDK ל-Admin

השירות Directory SDK Directory מאפשר להשתמש ב-Directory API של Apps SDK ב-Apps Script. ממשק ה-API הזה מאפשר לאדמינים של דומיינים Google Workspace (כולל מפיצים) לנהל מכשירים, קבוצות, משתמשים וישויות אחרות בדומיינים שלהם.

מקורות מידע

למידע מפורט על השירות הזה, קראו את מסמכי התיעוד של Admin SDK Directory. בדומה לכל השירותים המתקדמים ב-Apps Script, שירות ה-SDK של Admin SDK משתמש באותם אובייקטים, שיטות ופרמטרים כמו של ה-API הציבורי. מידע נוסף זמין במאמר איך נקבעות חתימות של שיטות.

כדי לדווח על בעיות ולמצוא תמיכה אחרת, תוכלו להיעזר במדריך התמיכה ל-Admin SDK Directory.

קוד לדוגמה

הקוד לדוגמה שמופיע בהמשך משתמש בגרסה 1 של ה-API.

רשימה של כל המשתמשים

הדוגמה הזו מפרטת את כל המשתמשים בדומיין, ממוינים לפי שם פרטי.

Advanced/adminSDK.gs
/**
 * Lists all the users in a domain sorted by first name.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list
 */
function listAllUsers() {
  let pageToken;
  let page;
  do {
    page = AdminDirectory.Users.list({
      domain: 'example.com',
      orderBy: 'givenName',
      maxResults: 100,
      pageToken: pageToken
    });
    const users = page.users;
    if (!users) {
      console.log('No users found.');
      return;
    }
    // Print the user's full name and email.
    for (const user of users) {
      console.log('%s (%s)', user.name.fullName, user.primaryEmail);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

הזמנת משתמש

בדוגמה הזו, המשתמש מקבל את כתובת האימייל שלו ומתעד את כל הנתונים שלו כמחרוזת JSON.

Advanced/adminSDK.gs
/**
 * Get a user by their email address and logs all of their data as a JSON string.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get
 */
function getUser() {
  // TODO (developer) - Replace userEmail value with yours
  const userEmail = 'liz@example.com';
  try {
    const user = AdminDirectory.Users.get(userEmail);
    console.log('User data:\n %s', JSON.stringify(user, null, 2));
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log('Failed with error %s', err.message);
  }
}

הוספת המשתמש/ת

הדוגמה הזו מוסיפה משתמש חדש לדומיין, כולל רק את המידע הנדרש. הרשימה המלאה של שדות המשתמשים מופיעה במסמכי התיעוד של ה-API.

Advanced/adminSDK.gs
/**
 * Adds a new user to the domain, including only the required information. For
 * the full list of user fields, see the API's reference documentation:
 * @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
 */
function addUser() {
  let user = {
    // TODO (developer) - Replace primaryEmail value with yours
    primaryEmail: 'liz@example.com',
    name: {
      givenName: 'Elizabeth',
      familyName: 'Smith'
    },
    // Generate a random password string.
    password: Math.random().toString(36)
  };
  try {
    user = AdminDirectory.Users.insert(user);
    console.log('User %s created with ID %s.', user.primaryEmail, user.id);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log('Failed with error %s', err.message);
  }
}

יצירת כינוי

הדוגמה הזו יוצרת כינוי (כינוי) למשתמש.

Advanced/adminSDK.gs
/**
 * Creates an alias (nickname) for a user.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users.aliases/insert
 */
function createAlias() {
  // TODO (developer) - Replace userEmail value with yours
  const userEmail = 'liz@example.com';
  let alias = {
    alias: 'chica@example.com'
  };
  try {
    alias = AdminDirectory.Users.Aliases.insert(alias, userEmail);
    console.log('Created alias %s for user %s.', alias.alias, userEmail);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log('Failed with error %s', err.message);
  }
}

הצגת כל הקבוצות

הדוגמה הזו מפרטת את כל הקבוצות בדומיין.

Advanced/adminSDK.gs
/**
 * Lists all the groups in the domain.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/list
 */
function listAllGroups() {
  let pageToken;
  let page;
  do {
    page = AdminDirectory.Groups.list({
      domain: 'example.com',
      maxResults: 100,
      pageToken: pageToken
    });
    const groups = page.groups;
    if (!groups) {
      console.log('No groups found.');
      return;
    }
    // Print group name and email.
    for (const group of groups) {
      console.log('%s (%s)', group.name, group.email);
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
}

הוספת חבר קבוצה

הדוגמה הזו מוסיפה משתמש לקבוצה קיימת בדומיין.

Advanced/adminSDK.gs
/**
 * Adds a user to an existing group in the domain.
 * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/insert
 */
function addGroupMember() {
  // TODO (developer) - Replace userEmail value with yours
  const userEmail = 'liz@example.com';
  // TODO (developer) - Replace groupEmail value with yours
  const groupEmail = 'bookclub@example.com';
  const member = {
    email: userEmail,
    role: 'MEMBER'
  };
  try {
    AdminDirectory.Members.insert(member, groupEmail);
    console.log('User %s added as a member of group %s.', userEmail, groupEmail);
  } catch (err) {
    // TODO (developer)- Handle exception from the API
    console.log('Failed with error %s', err.message);
  }
}