我在Quickstart中使用了代码示例,并尝试使用示例插入事件 - https://developers.google.com/calendar/v3/reference/events/insert#examples
我检查了网上可用的资源,但似乎没有一个对我有用(将googleapis软件包降级到版本24.0.0或使用不同的方式编写插入事件)。 这是当前addEvents函数的代码 -
function addEvents(auth){
var event = {
"summary": "Google I/O 2015",
"location": "800 Howard St., San Francisco, CA 94103",
"description": "A chance to hear more about Google's developer products.",
"start": {
"dateTime": "2015-05-28T09:00:00-07:00",
"timeZone": "America/Los_Angeles",
},
"end": {
"dateTime": "2015-05-28T17:00:00-07:00",
"timeZone": "America/Los_Angeles",
},
"recurrence": [
"RRULE:FREQ=DAILY;COUNT=2"
],
"reminders": {
"useDefault": false,
"overrides": [
{"method": "email", "minutes": 24 * 60},
{"method": "popup", "minutes": 10},
],
},
};
//console.log(event)
var calendar = google.calendar("v3");
calendar.events.insert({
auth: auth,
calendarId: "primary",
resource: event,
}, function(err, event) {
if (err) {
console.log("There was an error contacting the Calendar service: " + err);
return;
}
console.log("Event created: %s", event.htmlLink);
});
}
它与listEvents函数 -
位于同一行function listEvents(auth) {
var calendar = google.calendar('v3');
calendar.events.list({
auth: auth,
calendarId: 'primary',
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
},
function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
return;
}
var events = response.items;
if (events.length == 0) {
console.log('No upcoming events found.');
}
else {
console.log('Upcoming 10 events:');
for (var i = 0; i < events.length; i++) {
var event = events[i];
var start = event.start.dateTime || event.start.date;
console.log('%s - %s', start, event.summary);
}
}
});
}
有人可以帮我解决这个问题吗?
谢谢!
答案 0 :(得分:0)
这些是解决问题所需要做的事情 -
var {google} = require('googleapis');
代替var google = require('googleapis');
撰写var OAuth2 = google.auth.OAuth2;
代替var googleAuth = require('google-auth-library');
不要npm install google-auth-library
。如果您这样做,请将其从package.json中删除,然后再次npm install
。
确保您拥有最新版本的googleapis
包。如果你不这样做 - 做npm install googleapis@27
来获得它。 (也将它添加到package.json中以备将来使用。)
对于任何回调 - 而不是response.items
,请执行response.data.items
。创建事件时,您不一定需要回调输出,但在列出事件时肯定需要它。所以不要忘记在那里进行这种改变。
一旦您遵循所有这些步骤,我认为不会有任何问题。如果还有,请回复此问题。我会尽力让它解决!