管理使用者帳戶

Directory API 提供用於建立、更新及刪除使用者的程式輔助方法。也可以取得個別使用者或符合指定條件的使用者名單。以下是一些基本的使用者作業範例。

建立使用者帳戶

您可以將使用者帳戶新增至任何 Google Workspace 帳戶的網域,在新增使用者帳戶之前,請確認網域擁有權

如果您已將個人 Gmail 帳戶升級為使用您網域名稱的網域名稱的企業電子郵件帳戶,則必須等到解鎖其他 Google Workspace 設定後,才能建立新的使用者帳戶。詳情請參閱已更新至 G Suite Basic 的 G Suite 企業電子郵件帳戶

如要使用您的其中一個網域建立使用者帳戶,請使用下列 POST 要求,並附上瞭解驗證和授權的說明。您可以在 OAuth 2.0 範圍清單中查看 Directory API 的可用範圍。如需要求查詢字串屬性,請參閱 users.insert() 方法。

POST https://admin.googleapis.com/admin/directory/v1/users

建立所有要求時,您都必須提供提供該項要求所需的資訊。如果您使用用戶端程式庫,他們會將所選語言的資料物件轉換為 JSON 資料格式物件。

JSON 要求

以下 JSON 顯示建立使用者的要求範例,如需要求與回應屬性的完整清單,請參閱 API 參考資料

{
"primaryEmail": "liz@example.com",
"name": {
 "givenName": "Elizabeth",
 "familyName": "Smith"
},
"suspended": false,
"password": "new user password",
"hashFunction": "SHA-1",
"changePasswordAtNextLogin": false,
"ipWhitelisted": false,
"ims": [
 {
  "type": "work",
  "protocol": "gtalk",
  "im": "liz_im@talk.example.com",
  "primary": true
 }
],
"emails": [
 {
  "address": "liz@example.com",
  "type": "home",
  "customType": "",
  "primary": true
 }
],
"addresses": [
 {
  "type": "work",
  "customType": "",
  "streetAddress": "1600 Amphitheatre Parkway",
  "locality": "Mountain View",
  "region": "CA",
  "postalCode": "94043"
 }
],
"externalIds": [
 {
  "value": "12345",
  "type": "custom",
  "customType": "employee"
 }
],
"organizations": [
 {
  "name": "Google Inc.",
  "title": "SWE",
  "primary": true,
  "type": "work",
  "description": "Software engineer"
 }
],
"phones": [
 {
  "value": "+1 nnn nnn nnnn",
  "type": "work"
 }
],
"orgUnitPath": "/corp/engineering",
"includeInGlobalAddressList": true
}

如果建立要求的查詢頻率過高,您可能會收到 API 伺服器傳送的 HTTP 503 回應,指出配額已超出配額。如果你收到這些回應,請使用指數輪詢演算法重試要求。

新帳戶的相關注意事項如下:

  • 如果 Google 帳戶已購買郵件授權,系統會自動為新的使用者帳戶指派信箱。這項指派作業可能需要幾分鐘才能完成並完成。
  • API 服務會自動忽略要求 (例如 isAdmin) 中的唯讀欄位。
  • 每個帳戶允許的網域數量上限為 600 個 (1 個主網域,再加上 599 個額外的網域)
  • 使用者帳戶在建立時,如果使用者尚未指派給特定機構單位,則帳戶會位於頂層機構單位。使用者可存取哪些 Google Workspace 服務,取決於他們的機構單位。如果使用者移至新的機構,其存取權就會變更。如要進一步瞭解機構架構,請參閱管理員說明中心。如要進一步瞭解如何將使用者移至其他機構,請參閱更新使用者
  • 新使用者帳戶必須具有 password。指定 hashFunction 時,密碼必須是有效的雜湊金鑰。如未指定,密碼應採用純文字格式,長度必須介於 8 至 100 個 ASCII 字元。詳情請參閱 API 參考資料
  • 如果使用者採用 Google Workspace 彈性方案,建立使用這個 API 的使用者將會帶來金錢影響,而且系統會向您的客戶帳單帳戶收取相關費用。詳情請參閱 API 帳單資訊
  • Google Workspace 帳戶可包含你的任何網域。在多個網域中,一個網域中的使用者可以與其他帳戶網域中的使用者共用服務。如要進一步瞭解多個網域中的使用者,請參閱 API 多重網域資訊
  • 可能有相衝突的帳戶。確認您想新增的使用者是否已擁有 Google 帳戶。然後按照步驟操作,避免與這些帳戶衝突。請參閱尋找並解決相衝突的帳戶
  • 可能有訪客帳戶。如果使用者邀請機構外的 Google 帳戶使用者沒有在雲端硬碟中協同合作,他們將取得協作者帳戶,格式為:<訪客名稱>@<使用者名稱>@<您的網域>.com。如果您新增的使用者名稱與協作者帳戶相同,系統會將該帳戶轉換成完整的 Google Workspace 帳戶。這個帳戶會保留目前的雲端硬碟檔案權限。請參閱與協作者共用文件

成功的回應會傳回 HTTP 200 狀態碼。除了狀態碼之外,回應也會傳回新使用者帳戶的屬性。

更新使用者帳戶

如要更新使用者帳戶,請使用下列 PUT 要求,並加入授權要求中所述的授權。userKey 可以是使用者的主要電子郵件地址、不重複使用者使用者 id,或是使用者的別名電子郵件地址。

PUT https://admin.googleapis.com/admin/directory/v1/users/userKey

要求和回應主體都含有 User 的執行個體。不過,Directory API 支援修補語意,因此您只需要在要求中提交已更新的欄位即可。

要求範例

在以下範例中,使用者帳戶的 givenName 在建立時為「Elizabeth」,而且只提供公司的電子郵件地址。

{
  "name": {
    "givenName": "Elizabeth",
    "familyName": "Smith"
   },
  "emails": [
    {
      "address": "liz@example.com",
      "type": "work",
      "primary": true
    }
}

下方的要求會將 givenName 從「Elizabeth」更新為「Liz」,然後新增住家地址。請注意,這兩個欄位都是完整的,因為該欄位是陣列。

PUT https://admin.googleapis.com/admin/directory/v1/users/liz@example.com

{
  "name": {
    "givenName": "Liz",
   },
  "emails": [
    {
      "address": "liz@example.com",
      "type": "work",
      "primary": true
    },
    {
      "address": "liz@home.com",
      "type": "home"
    }
  ]
}

成功的回應會傳回包含已更新欄位的 HTTP 200 狀態碼User 資源。

更新使用者帳戶名稱時,請注意下列事項:

  • 重新命名使用者帳戶會變更使用者的主要電子郵件地址,以及擷取這位使用者資訊時使用的網域。重新命名使用者之前,建議您將使用者從所有瀏覽器工作階段和服務中登出。
  • 重新命名使用者帳戶的程序最多可能需要 10 分鐘才能套用到所有服務。
  • 當您重新命名使用者時,系統會將舊的使用者名稱保留為別名,以便在電子郵件轉寄設定的情況下,繼續傳送郵件,而且不會以新的使用者名稱使用。
  • 在一般情況下,我們不建議使用使用者電子郵件地址做為持續資料的金鑰,因為電子郵件地址可能會有變動。
  • 如需針對 Google Workspace 應用程式重新命名使用者的完整完整清單,請參閱管理員說明中心

將使用者設為管理員

如要讓使用者成為超級管理員,請使用下列 POST 要求,並附上授權要求中所述的授權。userKey 可以是使用者的主要電子郵件地址、不重複使用者身分 (id),或是使用者的別名電子郵件地址。如需要求與回應屬性,請參閱 API 參考資料。如要進一步瞭解超級管理員,請參閱管理員說明中心

POST https://admin.googleapis.com/admin/directory/v1/users/userKey/makeAdmin

JSON 要求

在這個範例中,userKey 為 liz@example.com 的使用者成為超級管理員:

POST https://admin.googleapis.com/admin/directory/v1/users/liz@example.com/makeAdmin
{
 "status": true
}

成功的回應會傳回 HTTP 200 狀態碼

管理使用者關係

Directory API 會使用 relations 欄位來定義使用者之間各種不同類型的關係。在商業設定中,使用者通常會將這個欄位用於管理員與助理和助理關係,但這個欄位也支援許多其他類型。任何與支援卡片的 Google Workspace 應用程式中,使用者的「相關人員」資訊卡都會顯示關係。如需關於資訊卡顯示位置的範例,請參閱為使用者目錄新增資訊

建立使用者之間的關係

您只能從一個「擁有權」使用者定義一個關係,其記錄包含 relations 欄位。type 說明其他使用者與自有使用者的關係。舉例來說,在管理員與員工的關係中,員工是自己擁有的使用者,而您在其 manager 類型的帳戶中新增 relations 欄位。如要瞭解允許的類型,請參閱 User 物件參考資料。

使用包含 relations 欄位的 JSON 要求主體建立更新自有使用者,藉此設定關係。 您可以在單一要求中建立多個關係。

{
  "relations": [
    {
      "value": "EMAIL_ADDRESS_RELATION_1",
      "type": "manager"
    },
    {
      "value": "EMAIL_ADDRESS_RELATION_2",
      "type": "dotted_line_manager"
    }
  ]
}

更新或刪除關係

您只能更新 relations 欄位,只能更新整體資訊,您無法處理列出的個別使用者來變更關係類型或移除這些類型。在上述範例中,如要移除現有的管理員關係,讓虛線管理員擁有其管理員,請更新自有使用者的帳戶,並依其保留所有欄位值。

{
  "relations": [
    {
      "value": "EMAIL_ADDRESS_RELATION_2",
      "type": "manager"
    }
  ]
}

如要移除使用者擁有的所有關係,請將 relations 設為空白:

{
  "relations": []
}

擷取使用者

如要擷取使用者,請使用下列 GET 要求,並加入授權要求中所述的授權。userKey 可以是使用者的主要電子郵件地址、不重複使用者身分 (id),或是使用者的別名電子郵件地址。如需要求與回應屬性,請參閱 API 參考資料

GET https://admin.googleapis.com/admin/directory/v1/users/userKey

本示例會傳回主要或別名電子郵件地址為 liz@example.com 的使用者使用者帳戶屬性:

GET https://admin.googleapis.com/admin/directory/v1/users/liz@example.com

JSON 回應

成功的回應會傳回 HTTP 200 狀態碼。除了狀態碼之外,回應也會傳回使用者帳戶的屬性。

{
 "kind": "directory#user",
 "id": "the unique user id",
 "primaryEmail": "liz@example.com",
 "name": {
  "givenName": "Liz",
  "familyName": "Smith",
  "fullName": "Liz Smith"
 },
 "isAdmin": true,
 "isDelegatedAdmin": false,
 "lastLoginTime": "2013-02-05T10:30:03.325Z",
 "creationTime": "2010-04-05T17:30:04.325Z",
 "agreedToTerms": true,
 "hashFunction": "SHA-1",
 "suspended": false,
 "changePasswordAtNextLogin": false,
 "ipWhitelisted": false,
 "ims": [
  {
   "type": "work",
   "protocol": "gtalk",
   "im": "lizim@talk.example.com",
   "primary": true
  }
 ],
 "emails": [
  {
   "address": "liz@example.com",
   "type": "home",
   "customType": "",
   "primary": true
  }
 ],
 "addresses": [
  {
   "type": "work",
   "customType": "",
   "streetAddress": "1600 Amphitheatre Parkway",
   "locality": "Mountain View",
   "region": "CA",
   "postalCode": "94043"
  }
 ],
 "externalIds": [
  {
   "value": "employee number",
   "type": "custom",
   "customType": "office"
  }
 ],
 "organizations": [
  {
   "name": "Google Inc.",
   "title": "SWE",
   "primary": true,
   "customType": "",
   "description": "Software engineer"
  }
 ],
 "phones": [
  {
   "value": "+1 nnn nnn nnnn",
   "type": "work"
  }
 ],
 "aliases": [
  "lizsmith@example.com",
  "lsmith@example.com"
 ],
 "nonEditableAliases": [
  "liz@test.com"
 ],
 "customerId": "C03az79cb",
 "orgUnitPath": "corp/engineering",
 "isMailboxSetup": true,
 "includeInGlobalAddressList": true
}

擷取網域中的所有使用者

如要擷取同一網域中的所有使用者,請使用下列 GET 要求,並附上授權要求中所述的授權。為方便閱讀,這個範例使用換行符號:

GET https://admin.googleapis.com/admin/directory/v1/users
?domain=primary domain name&pageToken=token for next results page
&maxResults=max number of results per page
&orderBy=email, givenName, or familyName
&sortOrder=ascending or descending
&query=email, givenName, or familyName:the query's value*

如需要求與回應屬性,請參閱 API 參考資料

JSON 回應

在此範例中,系統傳回的 example.com 網域使用者最多會傳回每個回應網頁 2 個使用者網域。此回應的後續追蹤清單中有 nextPageToken。根據預設,系統會按照使用者電子郵件地址的字母順序傳回 100 位使用者清單:

GET https://admin.googleapis.com/admin/directory/v1/users?domain=example.com&maxResults=2

成功的回應會傳回 HTTP 200 狀態碼。除了狀態碼之外,回應也會傳回 example.com 網域 (maxResults=2) 中的 2 個使用者帳戶:

{
 "kind": "directory#users",
 "users": [
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "primaryEmail": "liz@example.com",
   "name": {
    "givenName": "Liz",
    "familyName": "Smith",
    "fullName": "Liz Smith"
   },
   "isAdmin": true,
   "isDelegatedAdmin": false,
   "lastLoginTime": "2013-02-05T10:30:03.325Z",
   "creationTime": "2010-04-05T17:30:04.325Z",
   "agreedToTerms": true,
   "hashFunction": "SHA-1",
   "suspended": false,
   "changePasswordAtNextLogin": false,
   "ipWhitelisted": false,
   "ims": [
    {
     "type": "work",
     "protocol": "gtalk",
     "im": "lizim@talk.example.com",
     "primary": true
    }
   ],
   "emails": [
    {
     "address": "liz@example.com",
     "type": "work",
     "customType": "",
     "primary": true
    }
   ],
   "addresses": [
    {
     "type": "work",
     "customType": "",
     "streetAddress": "1600 Amphitheatre Parkway",
     "locality": "Mountain View",
     "region": "CA",
     "postalCode": "94043"
    }
   ],
   "externalIds": [
    {
     "value": "employee number",
     "type": "custom",
     "customType": "office"
    }
   ],
   "organizations": [
    {
     "name": "Google Inc.",
     "title": "SWE",
     "primary": true,
     "customType": "",
     "description": "Software engineer"
    }
   ],
   "phones": [
    {
     "value": "+1 nnn nnn nnnn",
     "type": "work"
    }
   ],
   "aliases": [
    "lizsmith@example.com",
    "lsmith@example.com"
   ],
   "nonEditableAliases": [
    "liz@test.com"
   ],
   "customerId": "C03az79cb",
   "orgUnitPath": "corp/engineering",
   "isMailboxSetup": true,
   "includeInGlobalAddressList": true
  },
  {
   "kind": "directory#user",
   "id": "user unique ID",
   "primaryEmail": "admin2@example.com",
   "name": {
    "givenName": "admin",
    "familyName": "two",
    "fullName": "admin two"
   },
   "isAdmin": true,
   "isDelegatedAdmin": true,
   "lastLoginTime": "2013-02-05T10:30:03.325Z",
   "creationTime": "2010-04-05T17:30:04.325Z",
   "agreedToTerms": true,
   "hashFunction": "SHA-1",
   "suspended": true,
   "suspensionReason": "ADMIN",
   "changePasswordAtNextLogin": false,
   "ipWhitelisted": false,
   "emails": [
    {
     "address": "admin2@example.com",
     "type": "work",
     "customType": "",
     "primary": true
    }
   ],
   "externalIds": [
    {
     "value": "contractor license number",
     "type": "custom",
     "customType": "work"
    }
   ],
   "aliases": [
    "second_admin@example.com"
   ],
   "nonEditableAliases": [
    "admin@test.com"
   ],
   "customerId": "C03az79cb",
   "orgUnitPath": "corp/engineering",
   "isMailboxSetup": true,
   "includeInGlobalAddressList": true
  }
 ],
 "nextPageToken": "next page token"
}

擷取所有帳戶使用者

如要擷取可納入多個網域的帳戶中的所有使用者,請使用下列 GET 要求,並附上授權要求中所述的授權。為方便閱讀,這個範例使用換行符號:

GET https://admin.googleapis.com/admin/directory/v1/users
?customer=my_customer or customerId&pageToken=token for next results page
&maxResults=max number of results per page
&orderBy=email, givenName, or familyName
&sortOrder=ascending or descending
&query=user attributes
  • customer 查詢字串是 my_customercustomerId 的值。
  • 使用字串 my_customer 來代表帳戶的 customerId
  • 經銷商管理員可使用經銷商客戶的customerId。針對 customerId,請在「擷取特定網域的所有使用者」要求中使用帳戶的主網域名稱。產生的回應會包含 customerId 值。
  • 選用的 orderBy 查詢字串可決定清單的排序依據是使用者的主要電子郵件地址、姓氏或名字。使用 orderBy 時,您也可以使用 sortOrder 查詢字串,以遞增或遞減順序列出結果。
  • 選用的 query 查詢字串可讓您搜尋使用者設定檔中的許多欄位,包括核心和自訂欄位。如需範例,請參閱「搜尋使用者」一文。

如需要求與回應屬性,請參閱 API 參考資料

在此範例中,帳戶管理員要求在帳戶中的所有使用者要求,且每個回應網頁上都有一個使用者項目。nextPageToken 會前往結果的後續頁面:

GET https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&maxResults=1

在本範例中,經銷商管理員要求使用 customerId 值為 C03az79cb 的經銷帳戶中的所有使用者。

GET https://admin.googleapis.com/admin/directory/v1/users?customer=C03az79cb&maxResults=1

JSON 回應

成功的回應會傳回 HTTP 200 狀態碼。除了狀態碼之外,回應也會傳回這個帳戶中的所有使用者:

{
 "kind": "directory#users",
 "users": [
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "username": "admin2@example.com",
   "name": {
    "givenName": "admin",
    "familyName": "two",
    "fullName": "admin two"
   },
   "isAdmin": true,
   "isDelegatedAdmin": true,
   "lastLoginTime": "2013-02-05T10:30:03.325Z",
   "creationTime": "2010-04-05T17:30:04.325Z",
   "agreedToTerms": true,
   "hashFunction": "SHA-1",
   "suspended": false,
   "changePasswordAtNextLogin": false,
   "ipWhitelisted": false,
   "emails": [
    {
     "address": "admin2@example.com",
     "type": "work",
     "customType": "",
     "primary": true
    }
   ],
   "externalIds": [
    {
     "value": "employee number",
     "type": "custom",
     "customType": "office"
    }
   ],
   "aliases": [
     "second_admin@example.com"
   ],
   "nonEditableAliases": [
     "another_admin@test.com"
   ],
   "customerId": "C03az79cb",
   "orgUnitPath": "/",
   "isMailboxSetup": true,
   "includeInGlobalAddressList": true
  },
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "username": "liz@example.com",
   "name": {
    "givenName": "Elizabeth",
    "familyName": "Smith",
    "fullName": "Elizabeth Smith"
   },
   "isAdmin": false,
   "isDelegatedAdmin": false,
   "lastLoginTime": "1336509883546",
   "creationTime": "1404802800000",
   "agreedToTerms": false,
   "hashFunction": "SHA-1",
   "suspended": false,
   "changePasswordAtNextLogin": false,
   "ipWhitelisted": false,
   "emails": [
    {
     "address": "liz@example.com",
     "type": "home",
     "customType": "",
     "primary": true
    }
   ],
   "externalIds": [
    {
     "value": "employee number",
     "type": "custom",
     "customType": "bank"
    }
   ],
   "relations": [
    {
     "value": "liz",
     "type": "friend",
     "customType": ""
    }
   ],
   "aliases": [
    "lizsmith@example.com",
    "lsmith@example.com"
   ],
   "nonEditableAliases": [
    "liz@test.com"
   ],
   "customerId": "C03az79cb",
   "orgUnitPath": "/",
   "isMailboxSetup": true,
   "includeInGlobalAddressList": true
  },
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "username": "test3@example.com",
   "name": {
    "givenName": "Tester",
    "familyName": "Three",
    "fullName": "Tester Three"
   },
   "isAdmin": false,
   "isDelegatedAdmin": false,
   "lastLoginTime": "1336509883546",
   "creationTime": "1404802800000",
   "agreedToTerms": true,
   "hashFunction": "SHA-1",
   "suspended": false,
   "changePasswordAtNextLogin": false,
   "ipWhitelisted": false,
   "emails": [
    {
     "address": "test@example.com",
     "type": "work",
     "customType": "",
     "primary": true
    }
   ],
   "externalIds": [
    {
     "value": "employee number",
     "type": "custom",
     "customType": "office"
    }
   ],
   "aliases": [
    "tester3@example.com"
   ],
   "nonEditableAliases": [
    "third@test.com"
   ],
   "customerId": "C03az79cb",
   "orgUnitPath": "/",
   "isMailboxSetup": true,
   "includeInGlobalAddressList": true
  },
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "username": "work_admin@example.com",
   "name": {
    "givenName": "Admin",
    "familyName": "Work",
    "fullName": "Admin Work"
   },
   "isAdmin": true,
   "isDelegatedAdmin": true,
   "lastLoginTime": "1336509883546",
   "creationTime": "1404802800000",
   "agreedToTerms": true,
   "hashFunction": "SHA-1",
   "suspended": false,
   "changePasswordAtNextLogin": false,
   "ipWhitelisted": false,
   "emails": [
    {
     "address": "work_admin@example.com",
     "type": "work",
     "customType": "",
     "primary": true
    }
   ],
   "externalIds": [
    {
     "value": "employee number",
     "type": "custom",
     "customType": "office"
    }
   ],
   "aliases": [
    "my_alias@example.com"
   ],
   "nonEditableAliases": [
    "other_alias@test.com"
   ],
   "customerId": "C03az79cb",
   "orgUnitPath": "/",
   "isMailboxSetup": true,
   "includeInGlobalAddressList": true
  }
 ],
 "nextPageToken": "NNNNN"
}

擷取最近刪除的使用者

如要擷取過去 10 天內從帳戶或任一網域所屬網域刪除的所有使用者,請使用下列 GET 要求,並附上授權要求中說明的授權。如要取消刪除使用者,請參閱取消刪除使用者一文。

如要從帳戶的主網域或子網域擷取過去 20 天內刪除的使用者,請使用下列 GET 要求。domain 查詢字串是網域的主網域名稱。如需使用者要求和回應屬性,請參閱 API 參考資料。為方便閱讀,這個範例使用換行符號:

GET https://admin.googleapis.com/admin/directory/v1/users
?domain=primary domain name&pageToken=token for next results page
&maxResults=max number of results per page
&showDeleted=true
若帳戶擁有多個網域,您可以擷取過去 20 天內由整個帳戶所刪除的使用者,並使用以下 GET 要求。為方便閱讀,這個範例使用換行符號:
GET https://admin.googleapis.com/admin/directory/v1/users
?customer=my_customer or customerId&pageToken=token for next results page
&maxResults=max number of results per page&showDeleted=true
  • customer 查詢字串是 my_customercustomerId 的值。
  • 帳戶管理員使用 my_customer 字串來代表帳戶的 customerId
  • 經銷商管理員可使用經銷商客戶的customerId。針對 customerId,請在「擷取特定網域的所有使用者」要求中使用帳戶的主網域名稱。產生的回應會包含 customerId 值。

如需要求與回應屬性,請參閱 API 參考資料

在這個範例中,帳戶管理員要求帳戶中的所有已刪除使用者:

GET https://admin.googleapis.com/admin/directory/v1/users?customer=my_customer&showDeleted=true

JSON 回應

成功的回應會傳回 HTTP 200 狀態碼。除了狀態碼之外,回應也會傳回過去 20 天內刪除的使用者帳戶。

{
 "kind": "directory#users",
 "users": [
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "primaryEmail": "user1@example.com"
  },
  {
   "kind": "directory#user",
   "id": "the unique user id",
   "primaryEmail": "user3@example.com"
  }
 ],
 "nextPageToken": "token for next page of deleted users"
}

擷取使用者的相片

API 會擷取一張相片縮圖,也就是最新的 Google 個人資料相片。如要擷取使用者的最後一張相片,請使用下列 GET 要求,並附上授權要求中所述的授權。userKey 可以是使用者的主要電子郵件地址、使用者 id,或是任何使用者的別名電子郵件。如需要求與回應屬性,請參閱 API 參考資料

GET https://admin.googleapis.com/admin/directory/v1/users/userKey/photos/thumbnail

在這個範例中,會傳回 liz@example.com 的最新相片:

GET https://admin.googleapis.com/admin/directory/v1/users/liz@example.com/photos/thumbnail

JSON 回應

成功的回應會傳回 HTTP 200 狀態碼

{
 "kind": "directory#user#photo",
 "id": "the unique user id",
 "primaryEmail": "liz@example.com",
 "mimeType": "the photo mime type",
 "height": "the photo height in pixels",
 "width": "the photo width in pixels",
 "photoData": "web safe base64 encoded photo data"
}

API 的網路安全 base64 編碼與 RFC 4648 'base64url' 類似。因此:

  • 斜線 (/) 字元會替換成底線 (_) 字元。
  • 加號 (+) 字元會取代連字號 (-) 字元。
  • 等號 (=) 字元會替換為星號 (*)。
  • 針對邊框間距,系統會使用半形句號 (.) 字元,而不是使用等號 (=) 做為邊框間距的 RFC-4648 baseURL 定義。這麼做是為了簡化網址剖析。
  • 無論相片尺寸為何,API 都會按比例縮小為 96x96 像素。

如果您需要從 JavaScript 建立相容的連結,Google Closure Library 包含以 Apache 授權方式發布的 Base64 編碼和解碼函式

以非管理員的身分擷取使用者

只有管理員才能修改使用者帳戶,但是網域中的任何使用者都能讀取使用者設定檔。非管理員使用者可以發出 viewType 參數等於 domain_publicusers.getusers.list 要求,藉此擷取使用者的公開設定檔。https://www.googleapis.com/auth/admin.directory.user.readonly 範圍非常適合此用途。

domain_public 檢視表可讓非管理員使用者存取一組標準的核心欄位。如果是自訂欄位,您可以在定義結構定義時選擇要將該欄位設為公開或私人。

更新使用者的相片

如要更新使用者的相片,請使用下列 PUT 要求,並加入授權要求中所述的授權。userKey 可以是使用者的主要電子郵件地址、使用者 id,或是任何使用者別名的電子郵件地址。如需要求與回應屬性,請參閱 API 參考資料

PUT https://admin.googleapis.com/admin/directory/v1/users/userKey/photos/thumbnail

在這個例子中,liz@example.com 相片已更新:

PUT https://admin.googleapis.com/admin/directory/v1/users/liz@example.com/photos/thumbnail
{
"photoData": "web safe base64 encoded photo data"
}

更新相片時,API 會忽略 heightwidth

JSON 回應

成功的回應會傳回 HTTP 200 狀態碼

{
 "kind": "directory#user#photo",
 "id": "the unique user id",
 "primaryEmail": "liz@example.com",
 "mimeType": "the photo mime type",
 "height": "the photo height in pixels",
 "width": "the photo width in pixels",
 "photoData": "web safe base64 encoded photo data"
}

刪除使用者的相片

如要刪除使用者的相片,請使用下列 DELETE 要求,並附上授權要求中說明的授權。userKey 可以是使用者的主要電子郵件地址、使用者 id,或是任何使用者別名的電子郵件地址。如需要求與回應屬性,請參閱 API 參考資料

DELETE https://admin.googleapis.com/admin/directory/v1/users/userKey/photos/thumbnail

刪除後,使用者的相片就不會顯示。在使用者的相片中,系統會改為顯示輪廓。

刪除使用者帳戶

如要刪除使用者帳戶,請使用下列 DELETE 要求,並加入授權要求中所述的授權。userKey 可以是使用者的主要電子郵件地址、不重複使用者身分 (id),或是使用者的別名電子郵件地址。如需要求與回應屬性,請參閱 API 參考資料

DELETE https://admin.googleapis.com/admin/directory/v1/users/userKey

在這個範例中,「liz@example.com」使用者帳戶遭到刪除:

DELETE https://admin.googleapis.com/admin/directory/v1/users/liz@example.com

成功的回應只會傳回 HTTP 200 狀態碼

刪除使用者前,請注意下列事項:

  • 刪除的使用者將無法再登入。
  • 如要進一步瞭解使用者帳戶刪除作業,請參閱管理員說明中心

取消刪除使用者帳戶

過去 20 天內被刪除的使用者必須符合特定條件,才能還原使用者帳戶

如要取消刪除使用者帳戶,請使用下列 POST 要求,並附上授權要求中說明的授權。userKey 是系統在「擷取過去 20 天內刪除的使用者」作業的回應中找到的不重複使用者 id。這項作業無法userKey 中使用使用者的主要電子郵件地址或使用者的其中一個別名電子郵件地址。如需要求與回應屬性,請參閱 API 參考資料

POST https://admin.googleapis.com/admin/directory/v1/users/userKey/undelete

在此範例中,使用者 liz@example.com 的未刪除。系統會還原這位使用者先前的所有帳戶資源:

POST https://admin.googleapis.com/admin/directory/v1/users/12309329403209438205/undelete

成功的回應只會傳回 HTTP 204 狀態碼。如要查看未刪除的使用者帳戶,請使用擷取使用者作業。