Node.js - Google 日历 API 在插入事件时显示错误 "Missing end time"
Node.js - Google Calendar API showing error "Missing end time" when inserting an event
我正在为 Node.js 使用 Google Calendar library 从 API 获取日历 ID 和事件列表。这按预期工作。但是当我尝试插入或修改事件时,我遇到了常见的(!)错误消息 "Missing end time"。
我正在尝试使用 Request - Simplified HTTP client 发送 POST 请求,而不是使用 Google 库或其他包。
这是我的代码片段:
const request = require('request');
// Update Event Title
function insertEventIntoCalendar(calendarId,accessToken){
let endDateTime = {
dateTime: '2018-07-03T10:25:00.000-07:00',//end,
timeZone: 'Asia/Dhaka'
},
startDateTime = {
dateTime: '2018-07-03T10:00:00.000-07:00', //start,
timeZone: 'Asia/Dhaka'
},
url = "https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token="+accessToken,
options = {
data: {
end: endDateTime,
start: startDateTime,
summery: 'ARG will win',
location: '800 Howard St., San Francisco, CA 94103',
attendees: [],
reminders: {
useDefault: true,
}
},
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json'
},
calendarId: 'primary'
}
request.post(url, options, function (err, res, body) {
console.log('err =>', err);
console.log('body =>', body);
})
}
这是我的 console.log 消息:
err => null
body => {
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Missing end time."
}
],
"code": 400,
"message": "Missing end time."
}
}
注意: 我已经关注了与 Google 日历 API 和 Node.js 相关的所有问题和答案,但仍然面临同样的错误.而且我没有找到任何未使用 Google 库的答案。
如果你尝试过这种方式,请分享你的想法,否则你可以建议我一个更好的方法,我不需要任何 Config 文件来获取或修改 Google 日历信息。
问题在于声明 options 变量。 Request - Simplified HTTP client 的文档说 Request body 的 json 数据应该命名为 'json'。所以 options 变量应该是,
options = {
url: url
json: {
end: endDateTime,
start: startDateTime,
summery: 'ARG will win',
location: '800 Howard St., San Francisco, CA 94103',
attendees: [],
reminders: {
useDefault: true,
}
}
}
并发送 POST 请求,代码应为:
request.post(options,function (err, res, body) {
console.log('err =>', err);
// console.log("res =>", res);
console.log("body =>", body);
})
我试过这种方法,效果很好:)
bonnopc 回答帮我解决了。对于到这里搜索节点 js + Google 日历 API 错误的人,这也适用于那里:
auth.getClient().then(a => {
calendar.events.patch({
json: {
summary: "bablabbla",
start: JSON.stringify(new Date()),
end: JSON.stringify(new Date())
},
auth: a,
calendarId: GOOGLE_CALENDAR_ID,
eventId: changedEvent.id
})
})
我正在为 Node.js 使用 Google Calendar library 从 API 获取日历 ID 和事件列表。这按预期工作。但是当我尝试插入或修改事件时,我遇到了常见的(!)错误消息 "Missing end time"。
我正在尝试使用 Request - Simplified HTTP client 发送 POST 请求,而不是使用 Google 库或其他包。
这是我的代码片段:
const request = require('request');
// Update Event Title
function insertEventIntoCalendar(calendarId,accessToken){
let endDateTime = {
dateTime: '2018-07-03T10:25:00.000-07:00',//end,
timeZone: 'Asia/Dhaka'
},
startDateTime = {
dateTime: '2018-07-03T10:00:00.000-07:00', //start,
timeZone: 'Asia/Dhaka'
},
url = "https://www.googleapis.com/calendar/v3/calendars/primary/events?access_token="+accessToken,
options = {
data: {
end: endDateTime,
start: startDateTime,
summery: 'ARG will win',
location: '800 Howard St., San Francisco, CA 94103',
attendees: [],
reminders: {
useDefault: true,
}
},
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + accessToken,
'Accept': 'application/json'
},
calendarId: 'primary'
}
request.post(url, options, function (err, res, body) {
console.log('err =>', err);
console.log('body =>', body);
})
}
这是我的 console.log 消息:
err => null
body => {
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Missing end time."
}
],
"code": 400,
"message": "Missing end time."
}
}
注意: 我已经关注了与 Google 日历 API 和 Node.js 相关的所有问题和答案,但仍然面临同样的错误.而且我没有找到任何未使用 Google 库的答案。
如果你尝试过这种方式,请分享你的想法,否则你可以建议我一个更好的方法,我不需要任何 Config 文件来获取或修改 Google 日历信息。
问题在于声明 options 变量。 Request - Simplified HTTP client 的文档说 Request body 的 json 数据应该命名为 'json'。所以 options 变量应该是,
options = {
url: url
json: {
end: endDateTime,
start: startDateTime,
summery: 'ARG will win',
location: '800 Howard St., San Francisco, CA 94103',
attendees: [],
reminders: {
useDefault: true,
}
}
}
并发送 POST 请求,代码应为:
request.post(options,function (err, res, body) {
console.log('err =>', err);
// console.log("res =>", res);
console.log("body =>", body);
})
我试过这种方法,效果很好:)
bonnopc 回答帮我解决了。对于到这里搜索节点 js + Google 日历 API 错误的人,这也适用于那里:
auth.getClient().then(a => {
calendar.events.patch({
json: {
summary: "bablabbla",
start: JSON.stringify(new Date()),
end: JSON.stringify(new Date())
},
auth: a,
calendarId: GOOGLE_CALENDAR_ID,
eventId: changedEvent.id
})
})