Sattanathan
Sattanathan

Reputation: 463

Set up Google Calendar V3 API Push notification via my Google Apps script

  1. I want to use Google CAL API v3 to watch multiple calendars and thus to update all those events from those calendars to a Master Calendar, so I need to use WATCH/PUSH notification in order that whenever there is a new event in those cal’s, those events shd be automatically be pushed/created in my master calendar.
  2. But, I am facing the following issue :Under APIs Explorer. Most of the services return 200 OK, except the Watch request not working.

Services > Calendar API v3 > calendar.events.watch

i got the below request and response

Request

POST https://www.googleapis.com/calendar/v3/calendars/mailid%40domain.com/events/watch?key={YOUR_API_KEY}
{
}

Response

400 OK
 {
"error": {
 "errors": [
  {
   "domain": "global",
   "reason": "required",
   "message": "entity.resource"
  }
 ],
 "code": 400,
 "message": "entity.resource"
}
}

And then i followed as per the below google documentation https://developers.google.com/google-apps/calendar/v3/push

Registering Your domain:

Step 1: Verify that you own the domain

Step 2: Register your domain

I verified my domain/notifications in google webmaster tools. My verified domain look like this:

http://my-domain.com/notifications

And have added my domain name in the webhooks under the PUSH in Google Developer Console, as explained in the doc.

Is there anything i am missing here, plz is there an issue in SSL certificate fr my domain, how can i atleast test in the API explorer, so that i get a return code 200 OK. After that, I need to implement this push/WATCH via Google Apps Script.

Is there any documentation for apps script to use push notification.

Any hint, tip will help me, I am tired with this for the past 1 week in trial and error method.

Upvotes: 0

Views: 4692

Answers (1)

Gerardo
Gerardo

Reputation: 3845

In the request that you posted the body is empty. In the documentation there is an example where you need to provide the calendar ID, the web_hook and other information:

{
  "id": "01234567-89ab-cdef-0123456789ab", // Your channel ID.
  "type": "web_hook",
  "address": "https://example.com/notifications", // Your receiving URL.
  ...
  "token": "target=myApp-myCalendarChannelDest", // (Optional) Your channel token.
  "expiration": 1426325213000 // (Optional) Your requested channel expiration time.
  }

Add this information when creating the push notification.

For using apps script you can use URLfetch service with the method "fetch()" then you would need to provide the parameters mentions and make the call as a POST.

Hope this helps.

UPDATE: I will write here to have more space.

Push notifications work in this way. -You create a push notification with the id, the resource you want to watch, the address where you want to receive the notification and the optional parameters.

-if successful, the server will watch that resource and will send a notification to the address mentioned in the request describing the changes when a change is made to the resource.

-The server needs to know that you received the notification, so from the address mentioned in #1, you need to tell the server that you received the notification returning a HTTP 200 response. (for this you need an app that is listening to the notifications and sending the responses)

for your questions 1.- As the server will make a HTTP request to a url (your web_hook address) Google requires it to be secure, that's why it is required to be HTTPS.

2.- Yes, without that url you won't be able to receive the notification.

3.- Google also needs to verify that the url you provided is actually yours, so you will need to provide the url, then Google will provide you a file, you have to make this file available in that url, then Google will call the url an will retrieve that file. Then you can prove that the URL is yours. I'm not sure if this step can be done with apps script. You can actually deploy a script as web app but i'm not sure if you can use it for this purpose.(here you can find more info and different ways to verify the url)

4.- That's created by yourself. In the documentation is pointed out the recommendations for it.

I hope this clarifies your questions.

You can check here how push notifications work, this example is for Drive but the concept is the same.

Upvotes: 0

Related Questions