บริการไดเรกทอรี Admin SDK
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
บริการไดเรกทอรีของ Admin SDK ช่วยให้คุณใช้ Directory API ของ Admin SDK ใน Apps Script ได้ API นี้ช่วยให้ผู้ดูแลระบบโดเมน Google Workspace (รวมถึงตัวแทนจำหน่าย) สามารถจัดการอุปกรณ์ กลุ่ม ผู้ใช้ และเอนทิตีอื่นๆ ในโดเมนของตนได้
ข้อมูลอ้างอิง
ดูข้อมูลโดยละเอียดเกี่ยวกับบริการนี้ได้ในเอกสารประกอบอ้างอิงสำหรับ Admin SDK
Directory API เช่นเดียวกับบริการขั้นสูงทั้งหมดใน Apps Script บริการไดเรกทอรีของ Admin SDK
จะใช้ออบเจ็กต์ เมธอด และพารามิเตอร์เดียวกันกับ
API สาธารณะ ดูข้อมูลเพิ่มเติมได้ที่วิธีกำหนดลายเซ็นของเมธอด
หากต้องการรายงานปัญหาและรับการสนับสนุนอื่นๆ โปรดดูคู่มือการสนับสนุนของ Admin SDK Directory
โค้ดตัวอย่าง
ตัวอย่างโค้ดด้านล่างใช้ API เวอร์ชัน 1
แสดงรายชื่อผู้ใช้ทั้งหมด
ตัวอย่างนี้แสดงผู้ใช้ทั้งหมดในโดเมนโดยเรียงตามชื่อ
ขอรับผู้ใช้
ตัวอย่างนี้จะรับผู้ใช้ตามอีเมลและบันทึกข้อมูลทั้งหมดของผู้ใช้เป็นสตริง JSON
เพิ่มผู้ใช้
ตัวอย่างนี้จะเพิ่มผู้ใช้ใหม่ในโดเมน โดยมีเฉพาะข้อมูลที่จำเป็น
ดูรายการฟิลด์ผู้ใช้ทั้งหมดได้ในเอกสารอ้างอิงของ API
สร้างอีเมลแทน
ตัวอย่างนี้สร้างชื่อแทน (ชื่อเล่น) สำหรับผู้ใช้
ระบุกลุ่มทั้งหมด
ตัวอย่างนี้แสดงกลุ่มทั้งหมดในโดเมน
เพิ่มสมาชิกกลุ่ม
ตัวอย่างนี้จะเพิ่มผู้ใช้ไปยังกลุ่มที่มีอยู่ในโดเมน
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThe Admin SDK Directory service enables Google Workspace administrators to manage domain resources like users, groups, and devices within Apps Script using the Directory API.\u003c/p\u003e\n"],["\u003cp\u003eThis advanced service requires enabling both the Admin SDK and the specific service before use, and it mirrors the functionality of the public Directory API.\u003c/p\u003e\n"],["\u003cp\u003eSample code snippets demonstrate common tasks like listing and managing users, creating aliases, and handling groups and their members via the Admin SDK Directory service.\u003c/p\u003e\n"],["\u003cp\u003eBefore using the sample code, remember to enable the Admin SDK, replace placeholder values with your specific data, and refer to the documentation for details on API usage and error handling.\u003c/p\u003e\n"]]],[],null,["# Admin SDK Directory Service\n\nThe Admin SDK Directory service allows you to use the Admin SDK's\n[Directory API](/admin-sdk/directory) in Apps Script. This API gives\nadministrators of Google Workspace domains (including\nresellers) the ability to\nmanage devices, groups, users, and other entities in their domains.\n| **Note:** This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced). Additionally, the Admin SDK must be enabled on your domain, as described in the API's [prerequisites documentation](/admin-sdk/directory/v1/guides/prerequisites).\n\nReference\n---------\n\nFor detailed information on this service, see the\n[reference documentation](/admin-sdk/directory/v1/reference) for the Admin SDK\nDirectory API. Like all advanced services in Apps Script, the Admin SDK\nDirectory service uses the same objects, methods, and parameters as the public\nAPI. For more information, see [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the\n[Admin SDK Directory support guide](/admin-sdk/directory/support).\n\nSample code\n-----------\n\nThe sample code below uses [version 1](/admin-sdk/directory/v1/reference) of\nthe API.\n\n### List all users\n\nThis sample lists all the users in a domain sorted by first name. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Lists all the users in a domain sorted by first name.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/list\n */\nfunction listAllUsers() {\n let pageToken;\n let page;\n do {\n page = AdminDirectory.Users.list({\n domain: 'example.com',\n orderBy: 'givenName',\n maxResults: 100,\n pageToken: pageToken\n });\n const users = page.users;\n if (!users) {\n console.log('No users found.');\n return;\n }\n // Print the user's full name and email.\n for (const user of users) {\n console.log('%s (%s)', user.name.fullName, user.primaryEmail);\n }\n pageToken = page.nextPageToken;\n } while (pageToken);\n}\n```\n\n### Get user\n\nThis sample gets a user by their email address and logs all of their data as a\nJSON string. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Get a user by their email address and logs all of their data as a JSON string.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get\n */\nfunction getUser() {\n // TODO (developer) - Replace userEmail value with yours\n const userEmail = 'liz@example.com';\n try {\n const user = AdminDirectory.Users.get(userEmail);\n console.log('User data:\\n %s', JSON.stringify(user, null, 2));\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n### Add user\n\nThis sample adds a new user to the domain, including only the required\ninformation. For the full list of user fields, see the API's\n[reference documentation](/admin-sdk/directory/v1/reference/users/insert). \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Adds a new user to the domain, including only the required information. For\n * the full list of user fields, see the API's reference documentation:\n * @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert\n */\nfunction addUser() {\n let user = {\n // TODO (developer) - Replace primaryEmail value with yours\n primaryEmail: 'liz@example.com',\n name: {\n givenName: 'Elizabeth',\n familyName: 'Smith'\n },\n // Generate a random password string.\n password: Math.random().toString(36)\n };\n try {\n user = AdminDirectory.Users.insert(user);\n console.log('User %s created with ID %s.', user.primaryEmail, user.id);\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n### Create alias\n\nThis sample creates an alias (nickname) for a user. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Creates an alias (nickname) for a user.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/users.aliases/insert\n */\nfunction createAlias() {\n // TODO (developer) - Replace userEmail value with yours\n const userEmail = 'liz@example.com';\n let alias = {\n alias: 'chica@example.com'\n };\n try {\n alias = AdminDirectory.Users.Aliases.insert(alias, userEmail);\n console.log('Created alias %s for user %s.', alias.alias, userEmail);\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```\n\n### List all groups\n\nThis sample lists all the groups in the domain. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Lists all the groups in the domain.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/groups/list\n */\nfunction listAllGroups() {\n let pageToken;\n let page;\n do {\n page = AdminDirectory.Groups.list({\n domain: 'example.com',\n maxResults: 100,\n pageToken: pageToken\n });\n const groups = page.groups;\n if (!groups) {\n console.log('No groups found.');\n return;\n }\n // Print group name and email.\n for (const group of groups) {\n console.log('%s (%s)', group.name, group.email);\n }\n pageToken = page.nextPageToken;\n } while (pageToken);\n}\n```\n\n### Add group member\n\nThis sample adds a user to an existing group in the domain. \nadvanced/adminSDK.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/adminSDK.gs) \n\n```javascript\n/**\n * Adds a user to an existing group in the domain.\n * @see https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/insert\n */\nfunction addGroupMember() {\n // TODO (developer) - Replace userEmail value with yours\n const userEmail = 'liz@example.com';\n // TODO (developer) - Replace groupEmail value with yours\n const groupEmail = 'bookclub@example.com';\n const member = {\n email: userEmail,\n role: 'MEMBER'\n };\n try {\n AdminDirectory.Members.insert(member, groupEmail);\n console.log('User %s added as a member of group %s.', userEmail, groupEmail);\n } catch (err) {\n // TODO (developer)- Handle exception from the API\n console.log('Failed with error %s', err.message);\n }\n}\n```"]]