Direct API calls to Google Calendar CalDAV endpoint without googleapis library in Node.js

I’m trying to make direct HTTP requests to Google Calendar’s CalDAV API without relying on the built-in functions from the googleapis package. Most tutorials I find online use the googleapis library’s helper methods, but I want to handle the raw API calls myself using just HTTP requests.

I know I need to handle OAuth authentication first, and I’m okay with using OAuth libraries for that part. But for actually reading and modifying calendar events, I want to make the HTTP calls directly.

Here’s what I’m attempting so far:

const http = require("http");
const { google } = require("googleapis");
require("dotenv").config();
const fetch = require("node-fetch");

http.createServer(async (request, response) => {
  const authClient = new google.auth.OAuth2(
    process.env.GOOGLE_CLIENT_ID,
    process.env.GOOGLE_CLIENT_SECRET,
    process.env.CALLBACK_URI
  );
  const accessToken = authClient.credentials.access_token;

  try {
    const calendarResponse = await fetch(`https://apidata.googleusercontent.com/caldav/v2/${calendarId}/events/`, {
      method: "PROPFIND",
      headers: {
        "Authorization": `Bearer ${accessToken}`,
        "Depth": "1",
        "Content-Type": "text/xml"
      },
      body: `
        <cal:calendar-query xmlns:d="DAV:" xmlns:cal="urn:ietf:params:xml:ns:caldav">
          <d:prop>
            <d:getetag />
            <cal:calendar-data />
          </d:prop>
          <cal:filter>
            <cal:comp-filter name="VCALENDAR">
              <cal:comp-filter name="VEVENT" />
            </cal:comp-filter>
          </cal:filter>
        </cal:calendar-query>
      `
    });

    console.log("Calendar data:", calendarResponse.data);
  } catch (err) {
    console.error("Failed to retrieve calendar:", err);
  }
});

Is this the right approach for accessing Google Calendar data through direct CalDAV requests?

Had the same issues last year. Google Calendar’s CalDAV endpoint URL is different - you need https://apidata.googleusercontent.com/caldav/v2/[email]/events/ where [email] is the calendar owner’s email, not just a calendar ID. Also, use REPORT instead of PROPFIND for calendar-query requests. Google’s CalDAV is picky about XML namespace declarations and request structure. Make sure your OAuth scope includes https://www.googleapis.com/auth/calendar and handle the response as text before parsing XML. Google’s CalDAV responses don’t always match the standard spec, so you’ll probably need to tweak your parsing logic. Your authentication looks right though.