在我的应用程序中发生某些事件后,是否可以将事件添加到用户的 google 日历(通过我的服务器)?

Is it possible to add events to a user's google calendar (via my server) after some event in my application?

我想知道是否可以通过 Firebase 将事件添加到用户的 google 日历服务器端。

我已阅读 this and this 这似乎是我想要实现的目标,但它解释了我想将活动添加到他们的日历的用户应该共享 他们的日历和我为我的应用程序创建的帐户。

这是真的还是我误会了什么?

如果有JavaScript/NodeJS的指导,我也很感激。

没有。 Firebase 没有任何用于将事件添加到 Google 日历的内置功能。但是连接两个 API 并不是特别困难。下面是一些额外的想法。

使用函数

一个优雅的解决方案是使用 Functions 通过以任何方式(HTTP、数据库写入等)触发 Functions 事件并相应地调用日历 API 来实现此目的。

步骤如下所示:

  1. 在客户端向 Google OAuth 进行身份验证时,添加日历范围 ('https://www.googleapis.com/auth/calendar')
  2. 触发函数时,发送日历负载和 Google OAuth 令牌

在 Cloud Functions for Firebase 中,您的触发器看起来像这样:

// Example combined from these docs:
//  https://developers.google.com/calendar/v3/reference/events/insert#examples
//  https://cloud.google.com/solutions/authentication-in-http-cloud-functions#writing_the_cloud_function
//  https://firebase.google.com/docs/functions/http-events

//using another name other than "google" seems to cause error!!
const {google} = require('googleapis');
const calendar = google.calendar('v3');
const functions = require('firebase-functions');

// This example assumes an HTTP call
exports.addToCalendar = functions.https.onRequest((req, res) => {
  const eventData = req.query.eventData;
  const accessToken = getAccessToken(req);
  return addToCalendar(eventData, accessToken).then(() => {
     res.stats(200).send('yay');
  }).catch(e => res.status(e.code).send({error: e.message}));
});

function addEventToGoogleCalendar(eventData, accessToken) {
  const authClient = getOauthClient(accessToken);
  return new Promise((resolve, reject) => {
    calendar.events.insert({
      auth: authClient,
      calendarId: 'primary',
      resource: eventData,
    }, function(err, event) {
      if (err) {
        console.error(err);
        reject(err);
      }
      else {
        resolve();
      }
    });
  });
}

function getOauthClient(accessToken) {
  var oauth = new google.auth.OAuth2();
  oauth.setCredentials({access_token: accessToken});
  return oauth;
}

function getAccessToken(req) {
  const header = req.get('Authorization');
  if (header) {
      var match = header.match(/^Bearer\s+([^\s]+)$/);
      if (match) {
          return match[1];
      }
  }
  return null;
}

下面是实时数据库和 Firestore 的一些替代函数触发器:

// Alternative: Realtime DB trigger
exports.addToCalendar = functions.database.ref('/addToCalendar/{pushId}')
  .onWrite((event) => {
    const data = event.data.val();
    return addToCalendar(data.eventData, data.token)
      // clear from queue after write
      //.then(() => event.ref().remove());
   });

// Alternative: Firestore DB trigger
exports.addToCalendar = functions.firestore.document('addToCalendar/{pushId}')
  .onCreate((event) => {
    const data = event.data.data();
    return addTocalendar(data.eventData, data.token)
      // clear from queue after write
      //.then(() => event.data.ref.remove());
  }); 

一个示例 eventData 对象看起来像这样:

var event = {
  'summary': 'Google I/O 2015',
  'location': '800 Howard St., San Francisco, CA 94103',
  'description': 'A chance to hear more about Google\'s developer products.',
  'start': {
    'dateTime': '2015-05-28T09:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'end': {
    'dateTime': '2015-05-28T17:00:00-07:00',
    'timeZone': 'America/Los_Angeles',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'lpage@example.com'},
    {'email': 'sbrin@example.com'},
  ],
  'reminders': {
    'useDefault': false,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
};

使用 Zapier

Zapier 提供了用于集成 Firebase 和 Google 日历的触发器:https://zapier.com/apps/firebase/integrations/google-calendar