0

Suspend or Unsuspend User

by
Published today

Suspend a user (blocks sign-in and mail delivery but keeps their data), or set `suspended` to false to restore access.

Script gworkspace Verified

The script

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

3
/**
4
 * Suspend or Unsuspend User
5
 * Suspend a user (blocks sign-in and mail delivery but keeps their data), or set `suspended` to false to restore access.
6
 */
7
export async function main(
8
  auth: RT.Gworkspace,
9
  user_key: string,
10
  suspended: boolean = true,
11
) {
12
  const response = await fetch(
13
    `https://admin.googleapis.com/admin/directory/v1/users/${encodeURIComponent(user_key)}`,
14
    {
15
      method: "PATCH",
16
      headers: {
17
        Authorization: `Bearer ${auth.token}`,
18
        "Content-Type": "application/json",
19
        Accept: "application/json",
20
      },
21
      body: JSON.stringify({ suspended }),
22
    },
23
  )
24

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

29
  return await response.json()
30
}
31