最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create and delete events in a Google Calendar from a Firebase function - Stack Overflow

programmeradmin5浏览0评论

I am building a app with Firebase Functions as a backend. The app schedules events for a small sports club but those events must also be synced to a Google Calendar for public usage. I would like to keep the Google Calendar updated via a function, and thus far I have this code:

async function updateCalendarOnTimesChange(timeId: string, data: DocumentData | null) {
    const authClient = await getAuthClient();
    google.options({ auth: #SOMETHING# });

    // Remove event from calendar to start with
    await calendar.events.delete({
        calendarId: calendarId,
        eventId: timeId,
    });

    if (!data) {
        return;
    }

    const eventTime = data.dateAndTime as Timestamp;
    const dateStart = eventTime.toDate();
    dateStart.setHours(data.timeStartHour);
    dateStart.setMinutes(data.timeStartMinutes);
    const dateEnd = eventTime.toDate();
    dateEnd.setHours(data.timeEndHour);
    dateEnd.setMinutes(data.timeEndMinutes);

    const event = {
        summary: data.description,
        start: {
            dateTime: dateStart,
            //timeZone: 'America/Los_Angeles',
        },
        end: {
            dateTime: dateEnd,
            //timeZone: 'America/Los_Angeles',
        },
    };

    // Create new event
    const response = await calendar.events.insert({
        calendarId: calendarId,
        requestBody: event,
    });
}

I do not know what to give the auth parameter? As it is a function that runs automaticly and not tied to the actual user creating the event, but tied to the sports club Google Calendar, how do I authenticate with the Google Calendar?

Am I on the right track here?

Thank you
Søren

I am building a app with Firebase Functions as a backend. The app schedules events for a small sports club but those events must also be synced to a Google Calendar for public usage. I would like to keep the Google Calendar updated via a function, and thus far I have this code:

async function updateCalendarOnTimesChange(timeId: string, data: DocumentData | null) {
    const authClient = await getAuthClient();
    google.options({ auth: #SOMETHING# });

    // Remove event from calendar to start with
    await calendar.events.delete({
        calendarId: calendarId,
        eventId: timeId,
    });

    if (!data) {
        return;
    }

    const eventTime = data.dateAndTime as Timestamp;
    const dateStart = eventTime.toDate();
    dateStart.setHours(data.timeStartHour);
    dateStart.setMinutes(data.timeStartMinutes);
    const dateEnd = eventTime.toDate();
    dateEnd.setHours(data.timeEndHour);
    dateEnd.setMinutes(data.timeEndMinutes);

    const event = {
        summary: data.description,
        start: {
            dateTime: dateStart,
            //timeZone: 'America/Los_Angeles',
        },
        end: {
            dateTime: dateEnd,
            //timeZone: 'America/Los_Angeles',
        },
    };

    // Create new event
    const response = await calendar.events.insert({
        calendarId: calendarId,
        requestBody: event,
    });
}

I do not know what to give the auth parameter? As it is a function that runs automaticly and not tied to the actual user creating the event, but tied to the sports club Google Calendar, how do I authenticate with the Google Calendar?

Am I on the right track here?

Thank you
Søren

Share Improve this question edited Mar 11 at 14:45 Neigaard asked Mar 11 at 14:35 NeigaardNeigaard 4,08413 gold badges53 silver badges92 bronze badges 12
  • It's complicated. As you anticipate, you should use a Service Account to authenticate to Google Calendar. Ideally, you would use a user-defined Service Account as the identity for your Cloud Function and this account would have appropriate OAuth2 Scopes (Google Calendar is not part of Cloud IAM) to permit it to interact with a Calendar. However, if the club's Calendar is part of Google Workspace, then you can use a domain-wide delegated Service Account (see doc). – DazWilkin Commented Mar 11 at 15:46
  • If not, then the non-documented (Google discouraged? hacky?) solution is to add the Service Account's email address ({foo}@{project}.iam.gserviceaccount) to the Calendar as a user with permissions to make changes to events. – DazWilkin Commented Mar 11 at 15:48
  • How would one do that last part? Together with googleapis? – Neigaard Commented Mar 11 at 16:52
  • See How to share Google calendar – DazWilkin Commented Mar 11 at 17:12
  • I mean from the functions code, do I need to do something there to authenticate this way? And, the iam.gserviceaccount email address, where do I find that? – Neigaard Commented Mar 11 at 17:57
 |  Show 7 more comments

1 Answer 1

Reset to default 0

I came across the article 'Integrating Firebase Cloud Functions with Google Calendar API,' which you might find helpful as an alternative for your use case, considering DazWilkin mentioned it's somewhat complicated. While the article is outdated, some developers still find it useful. It can provide you with insights into the process of connecting the two platforms.

发布评论

评论列表(0)

  1. 暂无评论