如何处理Google Calendar API推送通知? [英] How to handle Google Calendar API push notifications?

查看:229
本文介绍了如何处理Google Calendar API推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎对

X-Goog-Channel-Id是我选择的手表ID. X-Goog-Resource-Uri似乎仅是日历的链接.而且我不知道X-Goog-Resource-Id是什么.它既不是我的日历ID也不是我的活动ID.

我只想知道什么数据已更改?如何从推送通知中提取该信息?

我不在乎是否必须订阅应用程序创建的每个事件,或者仅订阅每个日历一次.

解决方案

当您收到推送通知时,它几乎不包含任何有用的信息.您必须向Google发送请求,以询问自上次同步以来发生了什么变化.阅读这篇文章以获取更多信息.

基本上,您想使用$_SERVER['HTTP_X_GOOG_CHANNEL_ID'](您生成的手表ID)和/或$_SERVER['HTTP_X_GOOG_RESOURCE_ID'](Google生成的手表ID)查找您创建手表的日历ID放在您自己的数据库中(创建手表时必须将其存储).

拥有日历ID后,您必须执行事件/列表请求以了解发生了什么变化.他们在该页面上有一个PHP示例,但是您必须添加syncToken并存储它.

尽管事件/手表位于事件"下",您实际上是在监听 all 日历中的事件,而不是更改特定事件中的事件(请勿被具有选项-它什么都不做).确保存储您返回的resourceId.您需要使用它来停止通知.

此外,手表会在一段时间后过期.而且您也无法刷新.我建议您在旧手表即将过期之前将其停一两周,然后再创建一个新手表.新的ID必须与旧的ID不同.

I can't seem to make sense of this article.

I'm trying to create 2-way syncing of events between my web app and Google Calendar. I've got my events successfully syncing to Google Calendar, but the push notifications Google sends me aren't of any use.

When I create a new event, and then I create a watch:

$channel = new Google_Service_Calendar_Channel();
$channel->setId($watchId);
$channel->setType('web_hook');
$channel->setAddress(URL::route('gapi.watchEvent'));
$channel->setResourceId($seg->gapi_event_id); // does this do anything??
$channel->setToken(json_encode(['segment_id'=>$seg->id]));
$channel->setParams([
    'ttl' => 2628000, // is this respected??
]);

$watchResp = $calendarService->events->watch($googleCalendarId, $channel);

Firstly, does $channel->setResourceId do anything? I want to listen to changes to a specific event, but always seems to give me notifications for the entire calendar.

The headers that Google sends me look like this:

"X-Goog-Channel-Id": "qJSdaxkKKkXXXXXXXXXX",
"X-Goog-Channel-Expiration": "Thu, 10 Sep 2015 01:45:57 GMT",
"X-Goog-Resource-State": "exists",
"X-Goog-Message-Number": "1811101",
"X-Goog-Resource-Id": "wBkuOkYwEhDiXXXXXXXXX",
"X-Goog-Resource-Uri": "https://www.googleapis.com/calendar/v3/calendars/9t4rmmb2rfdvcXXXXXXX@group.calendar.google.com/events?alt=json",

The X-Goog-Channel-Id is the watch id that I chose. X-Goog-Resource-Uri appears to just be a link to the calendar. And I don't know what X-Goog-Resource-Id. It doesn't appear to be either my calendar ID or my event ID.

All I want to know is what data changed? How do I extract that information out of the push notification?

I don't care if I have to subscribe to every event my app creates, or just once per calendar.

解决方案

When you get a push notification, it contains almost no useful information. You have to make a request back to Google to ask what's changed since your last sync. Read this article for more information.

Basically, you want to use $_SERVER['HTTP_X_GOOG_CHANNEL_ID'] (the watch ID you generated) and/or $_SERVER['HTTP_X_GOOG_RESOURCE_ID'] (the watch ID Google generates) to look up the calendar ID that you created a watch on in your own DB (you'll have to store this when you create the watch).

Once you have the calendar ID, you have to do an events/list request to find out what's changed. They have a PHP example on that page, but you'll have to add in the syncToken which you should also store.

Despite events/watch being under "events", you're really listening to all the events for a calendar, not changes in a specific event (don't be fooled by the PHP API which has a Google_Service_Calendar_Channel::setResourceId option -- it does nothing). Make sure to store the resourceId you get back. You'll need that to stop notifications.

Also, watches expire after some amount of time. And you can't refresh them either. I suggest you stop the old watch a week or two before its about to expire and create a new one. The new one must have a different ID than the old one.

这篇关于如何处理Google Calendar API推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆