google-calendar-apioauth2-playground

Getting a 500 error for google calendar request


I have a node app that I am running locally in a lamda function using netlify dev. I get a 500 response error, not sure why I am pretty certain I have all the right credentials. I don't know how else to debug this. Hope some one can help I read something about trying to send the response 3s then 6s so on but that still returned the exact same issue. This is my first time using the api so not exactly sure what to do.

Keep in mind I am using the developer playground as my oauth redirect and have generated a refresh token there

const { google } = require("googleapis")
const { oAuth2 } = google.auth


    const oAuth2Client = new oAuth2(
      "fdsfs.apps.googleusercontent.com",
      "fdsaf"
    )

    oAuth2Client.setCredentials({
      refresh_token:
        "1//0fdsfas",
    })

    // Create a new calender instance.
    const calendar = google.calendar({ version: "v3", auth: oAuth2Client })

    // Create a new event start date instance for temp uses in our calendar.
    const eventStartTime = new Date()
    eventStartTime.setDate(eventStartTime.getDay() + 2)

    // Create a new event end date instance for temp uses in our calendar.
    const eventEndTime = new Date()
    eventEndTime.setDate(eventEndTime.getDay() + 4)
    eventEndTime.setMinutes(eventEndTime.getMinutes() + 45)

    // Create a dummy event for temp uses in our calendar
    const event = {
      summary: `Meeting with David`,
      location: `3595 California St, San Francisco, CA 94118`,
      description: `Meet with David to talk about the new client project and how to integrate the calendar for booking.`,
      colorId: 1,
      start: {
        dateTime: eventStartTime,
        timeZone: "America/Denver",
      },
      end: {
        dateTime: eventEndTime,
        timeZone: "America/Denver",
      },
    }

    // Check if we a busy and have an event on our calendar for the same time.
    calendar.freebusy.query(
      {
        resource: {
          timeMin: eventStartTime,
          timeMax: eventEndTime,
          timeZone: "America/Denver",
          items: [{ id: "primary" }],
        },
      },
      (err, res) => {
        // Check for errors in our query and log them if they exist.
        if (err) return console.error("Free Busy Query Error: ", err)

        // Create an array of all events on our calendar during that time.
        const eventArr = res.data.calendars.primary.busy

        // Check if event array is empty which means we are not busy
        if (eventArr.length === 0)
          // If we are not busy create a new calendar event.
          return calendar.events.insert(
            { calendarId: "primary", resource: event },
            err => {
              // Check for errors and log them if they exist.
              if (err)
                return console.error("Error Creating Calender Event:", err)
              // Else log that the event was created.
              return console.log("Calendar event successfully created.")
            }
          )

        // If event array is not empty log that we are busy.
        return console.log(`Sorry I'm busy...`)
      }
    )

Solution

  • It was a simple mistake

    const { OAuth2 } = google.auth with capital O !not oAuth2