0

Get User

by
Published today

Retrieve a user by primary email, alias or unique ID. Use projection `full` to include custom schema fields. Works with admin.directory.user or admin.directory.user.readonly.

Script gworkspace Verified

The script

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

3
/**
4
 * Get User
5
 * Retrieve a user by primary email, alias or unique ID. Use projection `full` to include custom schema fields. Works with admin.directory.user or admin.directory.user.readonly.
6
 */
7
export async function main(
8
  auth: RT.Gworkspace,
9
  user_key: string,
10
  projection: "basic" | "full" = "basic",
11
) {
12
  const url = new URL(
13
    `https://admin.googleapis.com/admin/directory/v1/users/${encodeURIComponent(user_key)}`,
14
  )
15
  url.searchParams.append("projection", projection)
16

17
  const response = await fetch(url, {
18
    headers: {
19
      Authorization: `Bearer ${auth.token}`,
20
      Accept: "application/json",
21
    },
22
  })
23

24
  if (!response.ok) {
25
    throw new Error(`${response.status} ${await response.text()}`)
26
  }
27

28
  return await response.json()
29
}
30