且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过我的Google Apps脚本设置Google Calendar V3 API推送通知

更新时间:2023-02-14 12:20:35

在您发布的请求中,主体为空。在文档中有一个例子,您需要提供日历ID,web_hook和其他信息:

  {
id:01234567-89ab-cdef-0123456789ab,//您的频道ID。
type:web_hook,
address:https://example.com/notifications,//您的接收URL。
...
token:target = myApp-myCalendarChannelDest,//(可选)您的频道令牌。
expiration:1426325213000 //(可选)您请求的频道到期时间。

创建推送通知时添加此信息。



要使用应用脚本,您可以使用 URLfetch 一个>服务的方法fetch(),那么你需要提供参数提及,并将调用作为POST。



希望这有助于。



更新:我会在这里写下更多空间。



推送通知以这种方式工作。
- 您可以使用ID,要查看的资源,要接收通知的地址和可选参数创建推送通知。



如果成功,服务器将观察该资源,并在资源发生更改时向请求中提及的地址发送通知,以说明更改。



- 服务器需要知道您已收到通知,因此从#1中提到的地址开始,您需要告诉服务器您收到了返回HTTP 200响应的通知。 (为此,您需要一个正在侦听通知并发送响应的应用程序)

针对您的问题
1.-由于服务器会将HTTP请求网址(您的web_hook地址)谷歌要求它是安全的,这就是为什么它需要HTTPS。



2.-是的,没有这个网址你赢了无法收到通知。

3.- Google还需要验证您提供的网址实际上是您的网址,因此您需要提供网址,那么Google会为您提供一个文件,您必须在该网址中提供该文件,然后Google会调用该网址并检索该文件。然后你可以证明这个URL是你的。我不确定是否可以使用应用程序脚本完成此步骤。您实际上可以将脚本部署为Web应用程序,但我不确定您是否可以将其用于此目的。(这里你可以找到更多的信息和不同的方式来验证网址)



4.-这是你自己创建的。在文档中指出了它的建议。



我希望这可以澄清您的问题。



您可以在这里查看是如何工作的,这个例子适用于Drive,但概念是相同的。


  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.

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.