0

Get User

by
Published Oct 31, 2023

Get a user by gid, email or `me` (the token's own user, the default). The response lists the user's workspaces.

Script asana Verified

The script

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

3
/**
4
 * Get User
5
 * Get a user by gid, email or `me` (the token's own user, the default). The response lists the user's workspaces.
6
 */
7
export async function main(
8
  auth: RT.Asana,
9
  user_gid: string | undefined,
10
  opt_fields: string | undefined
11
) {
12
  const url = new URL(`https://app.asana.com/api/1.0/users/${user_gid || "me"}`)
13
  const params = { opt_fields }
14
  for (const [key, value] of Object.entries(params)) {
15
    if (value != null && value !== "") {
16
      url.searchParams.append(key, String(value))
17
    }
18
  }
19

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

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

31
  return await response.json()
32
}
33

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Asana = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get a user
    6
     * Returns the full user record for the single user with the provided ID.
    7
     */
    8
    export async function main(
    9
      auth: Asana,
    10
      user_gid: string,
    11
      opt_pretty: string | undefined,
    12
      opt_fields: string | undefined
    13
    ) {
    14
      const url = new URL(`https://app.asana.com/api/1.0/users/${user_gid}`);
    15
      for (const [k, v] of [
    16
        ["opt_pretty", opt_pretty],
    17
        ["opt_fields", opt_fields],
    18
      ]) {
    19
        if (v !== undefined && v !== "") {
    20
          url.searchParams.append(k, v);
    21
        }
    22
      }
    23
      const response = await fetch(url, {
    24
        method: "GET",
    25
        headers: {
    26
          Authorization: "Bearer " + auth.token,
    27
        },
    28
        body: undefined,
    29
      });
    30
      if (!response.ok) {
    31
        const text = await response.text();
    32
        throw new Error(`${response.status} ${text}`);
    33
      }
    34
      return await response.json();
    35
    }
    36