How to handle Google Calendar API push notifications?

后端 未结 1 1648
天命终不由人
天命终不由人 2020-12-16 18:13

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 s

相关标签:
1条回答
  • 2020-12-16 19:12

    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.

    0 讨论(0)
提交回复
热议问题