0

List Permissions

by
Published today

List who has access to a file or folder.

Script gdrive Verified

The script

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

3
/**
4
 * List Permissions
5
 * List who has access to a file or folder: each permission's id, type (user/group/domain/anyone), role, email or domain, and whether it is inherited from a parent folder. Use a permission id with Remove Permission.
6
 */
7
export async function main(
8
  auth: RT.Gdrive,
9
  file_id: string,
10
  page_size: number = 100,
11
  page_token: string | undefined
12
) {
13
  const url = new URL(
14
    `https://www.googleapis.com/drive/v3/files/${file_id}/permissions`
15
  )
16
  url.searchParams.append("supportsAllDrives", "true")
17
  url.searchParams.append("pageSize", String(page_size))
18
  url.searchParams.append(
19
    "fields",
20
    "nextPageToken, permissions(id, type, role, emailAddress, domain, displayName, allowFileDiscovery, expirationTime, deleted, permissionDetails)"
21
  )
22
  if (page_token != null && page_token !== "")
23
    url.searchParams.append("pageToken", page_token)
24

25
  const response = await fetch(url, {
26
    headers: {
27
      Authorization: `Bearer ${auth.token}`,
28
      Accept: "application/json",
29
    },
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38