Edits history of script submission #22380 for ' Subscribe to Watch Changes (gdrive)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    type Gdrive = {
      token: string;
    };
    export async function main(
      gdrive_auth: Gdrive,
      drive_id: string,
      webhook_address: string,
      channelId: any,
    ) {
      const SUPPORT_ALL_DRIVES = true;
      const SUPPORT_TEAM_DRIVES = true;
      const INCLUDE_ITEMS_FROM_ALL_DRIVES = true;
      const START_PAGE_URL = `https://www.googleapis.com/drive/v3/changes/startPageToken/?driveId=${drive_id}&supportsAllDrives=${SUPPORT_ALL_DRIVES}`;
    
      const token = gdrive_auth["token"];
    
      const response_sp = await fetch(START_PAGE_URL, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + token,
          "Content-Type": "application/json",
        },
      });
    
      const start_page_result = await response_sp.json();
      const START_PAGE_TOKEN = start_page_result["startPageToken"];
    
      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}`;
    
      const requestBody = {
        kind: "api#channel",
        type: "webhook",
        address: webhook_address,
        id: channelId,
      };
    
      const response = await fetch(WATCH_URL, {
        method: "POST",
        body: JSON.stringify(requestBody),
        headers: {
          Authorization: "Bearer " + token,
          "Content-Type": "application/json",
        },
      });
    
      const message = await response.json();
    
      return message;
    }
    

    Submitted by hugo989 7 days ago