0

List Groups

by
Published today

List or search the groups of the Workspace account (e.g. query `name:sales*`), or set `user_key` to list only the groups a user belongs to. Returns `groups` and `nextPageToken`. Works with admin.directory.group or admin.directory.group.readonly.

Script gworkspace Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * List Groups
5
 * List or search the groups of the Workspace account (e.g. query `name:sales*`), or set `user_key` to list only the groups a user belongs to. Returns `groups` and `nextPageToken`. Works with admin.directory.group or admin.directory.group.readonly.
6
 */
7
export async function main(
8
  auth: RT.Gworkspace,
9
  user_key: string | undefined,
10
  query: string | undefined,
11
  domain: string | undefined,
12
  max_results: number = 200,
13
  page_token: string | undefined,
14
) {
15
  const url = new URL("https://admin.googleapis.com/admin/directory/v1/groups")
16
  // With userKey alone Google returns that user's groups; otherwise it needs domain or customer
17
  if (domain !== undefined && domain !== "") {
18
    url.searchParams.append("domain", domain)
19
  } else if (user_key === undefined || user_key === "") {
20
    url.searchParams.append("customer", "my_customer")
21
  }
22
  for (const [k, v] of Object.entries({
23
    userKey: user_key,
24
    query,
25
    pageToken: page_token,
26
  })) {
27
    if (v !== undefined && v !== "") url.searchParams.append(k, v)
28
  }
29
  url.searchParams.append("maxResults", String(max_results))
30

31
  const response = await fetch(url, {
32
    headers: {
33
      Authorization: `Bearer ${auth.token}`,
34
      Accept: "application/json",
35
    },
36
  })
37

38
  if (!response.ok) {
39
    throw new Error(`${response.status} ${await response.text()}`)
40
  }
41

42
  return await response.json()
43
}
44