[英]Inserting an event with the nodejs google calendar API returns 400: “Missing end time”
[英]Node.js - Google Calendar API showing error "Missing end time" when inserting an event
我正在使用 Node.js 的Google 日历库从 API 获取日历 ID 和事件列表。这按预期工作。 但是当我尝试插入或修改事件时,我遇到了常见的 (.) 错误消息“缺少结束时间”。
我正在尝试使用Request - Simplified HTTP 客户端发送 POST 请求,而不是使用 Google Library 或其他 package。
这是我的代码片段:
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 Calendar API 和 Node.js 相关的所有问题和答案,但仍然面临同样的错误。 而且我还没有找到任何不使用谷歌图书馆的答案。
如果您尝试过这种方式,请分享您的想法,否则您可以建议我一个更好的方法,我不需要任何配置文件来获取或修改 Google 日历信息。
问题在于声明options变量。 Request-简化的HTTP客户端的文档说Request正文的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 的回答帮我解决了。 对于到这里搜索 node.js + Google Calendar 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
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.