Edits history of script submission #23030 for ' Query Free/Busy (gcal)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Query Free/Busy
     * Return the busy intervals of one or more calendars (ids or email addresses; `primary` for the connected account) between two RFC 3339 timestamps, e.g. 2026-10-01T09:00:00Z. Needs the calendar.freebusy (or calendar.readonly) scope, which the calendar.events-only connection lacks.
     */
    export async function main(
      auth: RT.Gcal,
      time_min: string,
      time_max: string,
      calendar_ids: string[] = ["primary"],
      time_zone: string | undefined
    ) {
      const body: { [key: string]: any } = {
        timeMin: time_min,
        timeMax: time_max,
        items: calendar_ids.map((id) => ({ id })),
      }
      if (time_zone !== undefined && time_zone !== "") body.timeZone = time_zone
    
      const response = await fetch(
        "https://www.googleapis.com/calendar/v3/freeBusy",
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify(body),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 1 hour ago