0

Reset User Password

by
Published today

Set a new password for a user (8 to 100 characters) and, by default, force them to change it at next sign-in. Existing sessions are not signed out.

Script gworkspace Verified

The script

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

3
/**
4
 * Reset User Password
5
 * Set a new password for a user (8 to 100 characters) and, by default, force them to change it at next sign-in. Existing sessions are not signed out.
6
 */
7
export async function main(
8
  auth: RT.Gworkspace,
9
  user_key: string,
10
  password: string,
11
  change_password_at_next_login: boolean = true,
12
) {
13
  const response = await fetch(
14
    `https://admin.googleapis.com/admin/directory/v1/users/${encodeURIComponent(user_key)}`,
15
    {
16
      method: "PATCH",
17
      headers: {
18
        Authorization: `Bearer ${auth.token}`,
19
        "Content-Type": "application/json",
20
        Accept: "application/json",
21
      },
22
      body: JSON.stringify({
23
        password,
24
        changePasswordAtNextLogin: change_password_at_next_login,
25
      }),
26
    },
27
  )
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  return await response.json()
34
}
35