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?