我正在尝试收听我使用 watch 请求创建的 Google 日历活动,但我不知道如何在收到推送通知后获取活动详细信息。
这就是我到目前为止所拥有的,我使用 JavaScript 创建日历事件(它提示用户授权):
function createEvent(data) {
var request = gapi.client.calendar.events.insert({
'calendarId': 'primary',
'resource': data
});
request.execute(function(event) {
if (!event.code) {
openToast('The event was added to your Google Calendar')
watchEvent(data.id);
}
});
}
然后我创建一个 watch 请求来获取推送通知:
function watchEvent(eventId) {
gapi.client.calendar.events.watch({
auth: gapi.auth2.getAuthInstance(),
resource: {
id: eventId,
type: 'web_hook',
address: webhookUrl
},
calendarId: 'primary'
}).execute(function onWatchCalendarEvent(event) {
if (event.error) {
console.error('Error creating event watch webhook', event.error);
}
});
}
到目前为止一切顺利,一切正常,我收到一条带有
channelID
的推送通知,从中我可以获取自己的内部 meetingId
。
@PostMapping
public void onCalendarEventUpdate(@RequestHeader(name = "x-goog-channel-id") String channelId,
@RequestHeader(name = "x-goog-resource-state") String resourceState) {
log.info("Received Google Calendar event update notification: " + resourceState);
// only when the event is modified google posts the resource state as "exists"
// (what kind of name is that anyway?)
if (resourceState.equals("exists")) {
// get the meeting ID from the channel ID
Long meetingId = findMeetingIdFromChannelId(channelId);
calendarService.updateMeeting(meetingsService.findById(meetingId));
}
}
我已经阅读了授权请求、推送通知和Java API入门的文档,但它们都提示用户授权。由于此推送通知是异步发生的,我应该如何获取更新的事件详细信息?谢谢
如果您有日历和事件 ID(例如,从 Google 收到的通知,您在 webhookUrl 位置接收并解析),您可以进行日历 api 调用以获取所有事件详细信息:https://developers.google .com/calendar/api/v3/reference/events/get
你得到答案了吗? 还在纠结这个问题