Inserting an event with the nodejs google calendar

2019-07-12 14:22发布

问题:

I am trying to insert events via the Google Calendar API with a service account. The goal is to have one calendar that all users view: a server-to-server setup.

As of now, I can properly call the calendar API and list the events on said calendar:

var moment = require("moment"),
    googleapis = require("googleapis"),
    googleCal = googleapis.calendar("v3");
    serviceEmail = "********@developer.gserviceaccount.com",
    serviceKeyFile = "./key.pem";

var authClient = new googleapis.auth.JWT(
        serviceEmail,
        serviceKeyFile,
        null,
        ["https://www.googleapis.com/auth/calendar"]
    );

authClient.authorize(function (err, tokens) {
    if (err) {
        console.log(err);
    } else {
        googleCal.events.list({
            auth: authClient,
            calendarId: "********@gmail.com",
            fields: {
                items: ["end","start","summary"]
            }
        }, function (err, CL) {
            if (err) {
                console.log(err);
            } else {
                console.log(CL);
            }
        });
    }
})

This properly returns a JSON object that lists all the different objects on the calendar. However, when I try to insert an event directly below the googleCal.events.list call:

googleCal.events.insert({
    auth: authClient,
    calendarId: "primary",
    resources: {
        start: {
          dateTime: "2014-07-23T18:25:00.000-07:00",
          timeZone: "America/New_York"
        }, 
        end: {
          dateTime: "2014-07-23T19:25:00.000-07:00",
          timeZone: "America/New_York"
        }, 
        summary: "winning @ life",
        description: "winning @ life description"
    }
}, function (err, something) {
    if (err) {
        console.log(err);
    } else {
        console.log(something);
        // do something else
    }
})

The following 400 is returned:

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

How do I go about fixing this? The authorization is clearly working—I know because I've used up my unauthorized requests for the day and because I can list all the events. I have also specified an endTime. Why is the google calendar API telling me that I haven't?

回答1:

I think the problem is in the keyword "resources" on line 4 of your second snippet. Based on the documentation, it should be a "resource":

 * @param  {object} params.resource - Request body data