Edits history of script submission #23059 for ' List Groups (gworkspace)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * List Groups
     * 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.
     */
    export async function main(
      auth: RT.Gworkspace,
      user_key: string | undefined,
      query: string | undefined,
      domain: string | undefined,
      max_results: number = 200,
      page_token: string | undefined,
    ) {
      const url = new URL("https://admin.googleapis.com/admin/directory/v1/groups")
      // With userKey alone Google returns that user's groups; otherwise it needs domain or customer
      if (domain !== undefined && domain !== "") {
        url.searchParams.append("domain", domain)
      } else if (user_key === undefined || user_key === "") {
        url.searchParams.append("customer", "my_customer")
      }
      for (const [k, v] of Object.entries({
        userKey: user_key,
        query,
        pageToken: page_token,
      })) {
        if (v !== undefined && v !== "") url.searchParams.append(k, v)
      }
      url.searchParams.append("maxResults", String(max_results))
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 1 hour ago