1
Subscribe to Watch Changes
One script reply has been approved by the moderators Verified
Created by rossmccrann 718 days ago Viewed 9219 times
0
Submitted by rossmccrann Deno
Verified 718 days ago
1
type Gdrive = {
2
  token: string;
3
};
4
export async function main(
5
  gdrive_auth: Gdrive,
6
  drive_id: string,
7
  webhook_address: string,
8
  channelId: any,
9
) {
10
  const SUPPORT_ALL_DRIVES = true;
11
  const SUPPORT_TEAM_DRIVES = true;
12
  const INCLUDE_ITEMS_FROM_ALL_DRIVES = true;
13
  const START_PAGE_URL = `https://www.googleapis.com/drive/v3/changes/startPageToken/?driveId=${drive_id}&supportsAllDrives=${SUPPORT_ALL_DRIVES}`;
14

15
  const token = gdrive_auth["token"];
16

17
  const response_sp = await fetch(START_PAGE_URL, {
18
    method: "GET",
19
    headers: {
20
      Authorization: "Bearer " + token,
21
      "Content-Type": "application/json",
22
    },
23
  });
24

25
  const start_page_result = await response_sp.json();
26
  const START_PAGE_TOKEN = start_page_result["startPageToken"];
27

28
  const WATCH_URL = `https://www.googleapis.com/drive/v3/changes/watch/?pageToken=${START_PAGE_TOKEN}&supportsAllDrives=${SUPPORT_ALL_DRIVES}&supportsTeamDrives=${SUPPORT_TEAM_DRIVES}&includeItemsFromAllDrives=${INCLUDE_ITEMS_FROM_ALL_DRIVES}`;
29

30
  const requestBody = {
31
    kind: "api#channel",
32
    type: "webhook",
33
    address: webhook_address,
34
    id: channelId,
35
  };
36

37
  const response = await fetch(WATCH_URL, {
38
    method: "POST",
39
    body: JSON.stringify(requestBody),
40
    headers: {
41
      Authorization: "Bearer " + token,
42
      "Content-Type": "application/json",
43
    },
44
  });
45

46
  const message = await response.json();
47

48
  return message;
49
}
50