shu*_*uwi 1 javascript google-calendar-api google-api-nodejs-client
我正在尝试在Node.js中使用google-api-nodejs-client。我timeMin在freebusy查询中的参数格式有问题。根据docs,值应为ISO格式的timedate。我试过了,但出现错误。在该event.list方法中,ISO格式字符串可以正常工作。
查询freebusy代码:
function freeBusyStatus (auth, calendarId) {
const startDate = new Date('20 February 2018 12:00').toISOString()
const endDate = new Date('20 February 2018 13:00').toISOString()
const check = {
auth: auth,
// timeMin: '2018-02-20T12:00:00+03:00', //not working with same format too
timeMin: new Date('20 February 2018 12:00'),
timeMax: endDate,
items: [{id: calendarId}]
}
calendar.freebusy.query (check, function (err, response) {
if (err) {
console.log ('error: ' + err)
} else {
..some code..
}
})
}
Run Code Online (Sandbox Code Playgroud)
并events.list以ISO格式编码日期。此代码有效:
function listEvents(auth, calendarId) {
const eventList = {
auth: auth,
calendarId: calendarId,
timeMin: new Date('20 February 2018 12:00').toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}
calendar.events.list(eventList, function(err, response) {
if(err) {
console.log(err)
}
const events = response.data.items
if (events.length != 0) {
..some code..
}
})
}
Run Code Online (Sandbox Code Playgroud)
我认为问题在于日期格式。当我尝试发送日期为2018-02-20(无时间)的请求时,响应具有相同的错误Error: Missing timeMin parameter.
我究竟做错了什么?授权是通过JWT完成的。
做完了!我的错误在于要求的结构
function freeBusyStatus (auth, calendarId) {
const check = {
auth: auth,
resource: {
timeMin: startDate,
timeMax: endDate,
items: [{id: calendarId}]
}
}
const startDate = new Date('20 February 2018 12:00').toISOString()
const endDate = new Date('20 February 2018 13:00').toISOString()
calendar.freebusy.query (check, function (err, response) {
if (err) {
console.log ('error: ' + err)
} else {
..some code..
}
})
}
Run Code Online (Sandbox Code Playgroud)