Search code examples
javascriptnode.jscalendargoogle-calendar-apiwebhooks

How to see list of watch channels for a specific calender in google calendar api?


I am able to create events watch channel for getting notified on change in events.

const { google } = require('googleapis');

const oauth2Client = new google.auth.OAuth2(
    config.gauth.clientId,
    config.gauth.clientSecret,
    config.gauth.redirectUrl
);

let authCode = 'some auth code after authentication'
let { tokens } = await oauth2Client.getToken(authCode);
oauth2Client.setCredentials(tokens);
const calendar = google.calendar({ version: 'v3', auth: oauth2Client });

const watchResponse = await calendar.events.watch({
    calendarId: 'primary',
    resource: {
        id: 'some uniqe id',
        type: "web_hook",
        address: 'https://some-host.com/notification'
    },
})
console.log("watch resp", watchResponse);

I want to list all created chennels. I could not find any method in official documentation at https://developers.google.com/calendar/api/v3/reference/channels

How can I list channels which I created using calendar.events.watch()?


Solution

  • Currently there's no way to list all your notification channels. You can only create the watch endpoint or stop it. Since you can set your own IDs and channel tokens, you're supposed to keep track of them to figure out the source or purpose of each channel.

    My guess as to why this is the case is because the channels are pretty much disposable. Based on my tests the default expiration is 7 days and the maximum is 30 days, so you can't have permanent notification channels. They will all expire within 30 days at most and you have to keep creating new ones. The development team may have seen it as unnecessary, or not a priority, to list resources that are ephemeral by nature.

    There is one way that you can have an idea of how many channels have been created, though. The Reports API can list OAuth token events. This captures calls to calendar.events.watch, including the Application ID, and the user who authorized the call, so you can tell who has created channels in the past 30 days, and have an idea of how many could still be active. It doesn't include info on the specific channel IDs, or which calendar was affected, so you'd still have to track that. For an actual list method it may be worth it to suggest this as a feature request in Google's issue tracker.

    References