Search code examples
node.jsgoogle-calendar-api

What is causing this 400 return code from the Google API when working with Calendars


I am having some trouble with the Google APIs and trying to interact with Google Calendar. I'm getting a missing resource 400 error when attempting to insert a new calendar ID.

Below is a snippet of the error JSON (I can't include everything due to some sensitive elements), I don't know what missing resource it's complaining about.

code: 400,
errors: [
    {
      domain: 'global',
      reason: 'required',
      message: 'Missing resource.'
    }
]

The code that's causing the problem is here (some pieces are omitted):

const { google } = require('googleapis');
const { googlePrivateKey, googleClientEmail, googleProjectNumber } = require('../../config.json');
const SCOPES = ['https://www.googleapis.com/auth/calendar'];
//OMITTED SECTION
const jwtClient = new google.auth.JWT(
    googleClientEmail,
    './keyfile.json',
    googlePrivateKey,
    SCOPES,
);

const calendar = new google.calendar({
    version: 'v3',
    project: googleProjectNumber,
    auth: jwtClient,
});

calendar.calendarList.insert({
    id: name,
    // 'description': description,
});

Referenced variables are drawn from other surrounding code or from an existing config.json file (not included due to sensitivity of information contained within). The information in config.json is known working (I was able to make a request to retrieve the list of calendars with a 200 response using another piece of code not shown here).

I've looked at as much documentation I can find, but I can't seem to discover any information on what I'm missing/doing wrong.


Solution

  • In your script, how about the following modification?

    From:

    calendar.calendarList.insert({
        id: name,
        // 'description': description,
    });
    

    To:

    const name = "###"; // Please set your calendar ID.
    calendar.calendarList.insert(
      {
        resource: { id: name },
      },
      (err, res) => {
        if (err) {
          console.log(err.errors);
          return;
        }
        console.log(res.data);
      }
    );
    
    • Please set your expected calendar ID to name. If this value is not correct, an error like "'Not Found" occurs. Please be careful about this.

    Reference: