Next.js Discord

Discord Forum

Google Calendar Oauth Error

Unanswered
Wuchang bream posted this in #help-forum
Open in Discord
Avatar
Wuchang breamOP
heyo all, I'm trying to code a Google calendar integration but really struggling with an error - I'm unsure if this is the right place to post it since it isn't directly next js related but here it is

  useEffect(() => {
    async function getCalendarData() {
      const data: any = await fetchEvents();
      setAvailability(data);
    }

    getCalendarData();
  }, []);


"use server";

const { google } = require("googleapis");
import { calendar_v3 } from "googleapis/build/src/apis/calendar";
const { GoogleAuth } = require("google-auth-library");
const auth = new GoogleAuth({
  keyFile: "/workspaces/eleven-plus-stars/src/lib/token.json",
  scopes: ["https://www.googleapis.com/auth/calendar"],
});

const calendar = google.calendar({ version: "v3", auth });
type times = {
  start: string;
  end: string;
  taken: boolean;
};

type formattedEvents = {
  date: string;
  times: times[];
};
export async function fetchEvents() {
  const res = await calendar.events.list({
    calendarId: "myemail.com",
    timeMin: new Date().toISOString(),
    maxResults: 10,
    singleEvents: true,
    orderBy: "startTime",
  });

  const events = res.data.items;

  if (events.length) {
    const filteredEvents = events.filter((event: calendar_v3.Schema$Event) =>
      event.summary?.startsWith("APPOINTMENT")
    );

    const eventsByDate: Record<string, times[]> = {};

    // Loop through the filtered events and populate the map
    filteredEvents.forEach((event: calendar_v3.Schema$Event) => {
      const startTime = event.start?.dateTime || event.start?.date;
      const endTime = event.end?.dateTime || event.end?.date;

      if (startTime && endTime) {
        // Extract the date (only the date part, ignoring the time)
        const date = new Date(startTime).toLocaleDateString("en-GB");
        console.log(date);

        // Create a time object
        const timeSlot: times = {
          start: new Date(startTime).toLocaleTimeString("en-GB", {
            hour: "2-digit",
            minute: "2-digit",
            hour12: false,
          }),
          end: new Date(endTime).toLocaleTimeString("en-GB", {
            hour: "2-digit",
            minute: "2-digit",
            hour12: false,
          }),
          taken: !!event.attendees, // Assume if there are attendees, the slot is taken
        };

        // Add the time slot to the corresponding date group
        if (!eventsByDate[date]) {
          eventsByDate[date] = [];
        }
        eventsByDate[date].push(timeSlot);
      }
    });

    // Convert the grouped events into the desired formattedEvents array
    const formattedEventList: formattedEvents[] = Object.keys(eventsByDate).map(
      (date) => ({
        date,
        times: eventsByDate[date],
      })
    );

    return formattedEventList;
  } else {
    console.log("No upcoming events found.");
  }
}


⨯ Error: request to https://www.googleapis.com/oauth2/v4/token failed, reason: The "options.flush" property must be of type number. Received undefined


I've tried googling up on the error, however I literally can't find anything

It works when

a) I run the file directly on my local machine (wsl) (so I just call the function inside the file and run bun googleCalenendarUtils.ts)

b) I run the next js app and call the function from within my client component in a use effect to fetch data

c) I run the file directly on a GitHub Code Space

However it doesn't work (I get the error sent above) when I run it from a component in my next js app in GitHub Code Spaces

If anyone here has any idea on what could be wrong, then I'd really appreciate your help - and if you need to see more code I can send some more

5 Replies

Avatar
Black carp
That could be an environment issue? Are the environment variables you have set that maybe aren't in GH?
Avatar
Wuchang breamOP
I thought this however it turned out to be an issue with bun
I asked in their discord server and they've made a fix and it should be in v1.1.28
I figured out it was a problem with bun since as a last resort I tried running it with node js and it worked fine however it wouldn't work in bun