0

Update User

by
Published today

Update a user's fields with patch semantics: only the fields in `body` change (e.g. `{"name": {"givenName": "Jane"}, "orgUnitPath": "/Sales", "suspended": false}`). Repeated fields like `phones` or `emails` are replaced as a whole.

Script gworkspace Verified

The script

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

3
/**
4
 * Update User
5
 * Update a user's fields with patch semantics: only the fields in `body` change (e.g. `{"name": {"givenName": "Jane"}, "orgUnitPath": "/Sales", "suspended": false}`). Repeated fields like `phones` or `emails` are replaced as a whole.
6
 */
7
export async function main(
8
  auth: RT.Gworkspace,
9
  user_key: string,
10
  body: { [key: string]: any },
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(body),
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