Google javascript API library - Calendar watch notifications

帅比萌擦擦* 提交于 2019-12-22 08:41:08

问题


I am trying to subscribe to calendar event notifications with the JS client library like this:

gapi.client.load('calendar', 'v3', function () {
            var request = gapi.client.calendar.events.watch({
                'calendarId': cl.gCalendarID,
                'id': unid,
                'type': "web_hook",
                'address': {my url here}
            });
            request.execute(function (resp) {
                console.log(resp);
            });
        });

But I just keep getting 400 returned with an unhelpful message of "Entity.Resource"

In the data object of the response I get Domain:global, Message: Entity.Resource, reason: Required"

I am authenticated already with oauth2 and I have granted access with my Google account and I can successfully retrieve the list of calendars and I am retrieving events from them calendars but this method to subscribe to the watch will not work? Please help I can't find anything on Google about this.


回答1:


Instead of this:


 var request = gapi.client.calendar.events.watch({
                'calendarId': cl.gCalendarID,
                'id': unid,
                'type': "web_hook",
                'address': {my url here}
            });

You Use this syntax:

calendar.events.watch({
            auth: auth,
                resource: {
                    id: "12345",
                    type: 'web_hook',
                    address: {mu url here}
                 },
                calendarId: 'primary' 

            }, function(err, response) {
                if (err) {
                    logger.MessageQueueLog.log("info","index.js:- watchnotification(),Error in Watch Notification: " + err);

                } else {          
                   logger.MessageQueueLog.log("info","index.js:- watchnotification(), Notification : " + JSON.stringify(response));           

                }
            });

Hope it will work.




回答2:


You need to pass the token value in the request made to the Calendar API, to distinguish your requests from each other as below

{
  "id": string,
  "token": string,
  "type": string,
  "address": string,
  "params": {
  "ttl": string
 }
}

For further reference, please refer to

Google Calendar API



来源:https://stackoverflow.com/questions/19320678/google-javascript-api-library-calendar-watch-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!