本文介绍了在应用程序中发生某些事件后,是否可以将事件添加到用户的Google日历中(通过我的服务器)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚是否可以通过Firebase将事件添加到用户的Google日历服务器端。

我已阅读和这似乎是我想要实现的,但它解释了我想要的用户向他们的日历中添加事件应与我为我的应用程序创建的帐户共享
日历。

这是真的还是我误解了一些东西?



如果有任何JavaScript / NodeJS指南,我也很感激。

解决方案

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

使用函数

可以使用通过任何方式触发函数事件(HTTP,a数据库写入等)并相应地调用Calendar API。

这些步骤如下所示:


  1. 在客户端上对Google OAuth进行身份验证时,添加日历范围('')

  2. 触发此功能时,请发送日历有效内容和Google OAuth令牌

Firebase内部的云端函数,您的触发器将如下所示:

  //从这些文档合并的示例:
// https://developers.google.com/calendar/v3/refer ence / 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

//使用除google之外的其他名称似乎会导致错误!
const {google} = require('googleapis');
const calendar = google.calendar('v3');
const函数= require('firebase-functions');

//这个例子假设一个HTTP调用
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);
返回新的Promise((resolve,reject)=> {
calendar.events.insert({
auth:authClient,
calendarId:'primary',
资源:eventData,
},函数(err,event){
if(err){
console.error(err);
reject(err);
}
else {
resolve();
}
});
});
}

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

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

$ / code>

这里有一些用于实时数据库和Firestore的替代函数触发器:

  //可选:实时数据库触发器
exports.addToCalendar = functions.database.ref('/ addToCalendar / {pushId}')
.onWrite((event)=> {
const data = event.data.val();
return addToCalendar(data.eventData,data.token)
// ()=> event.ref()。remove());
});

//可选:Firestore DB触发器
exports.addToCalendar = functions.firestore.document('addToCalendar / {pushId}')
.onCreate((event)=> {
const data = event.data.data();
return addTocalendar(data.eventData,data.token)
//写入
之后从队列中清除// (()=> event.data.ref.remove());
});

一个eventData对象的例子如下所示:

  var event = {
'summary':'Google I / O 2015',
'location':'800 Howard St.,旧金山, CA 94103',
'description':'有机会听到更多关于Google的开发者产品的信息。',
'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'
],
'参加者':[
{'email':'lpage@example.com'},
{'email' :'sbrin@example.com'},
],
'reminders':{$ b $'useDefault':false,
'覆盖':[
{方法':'电子邮件','分钟':24 * 60},
{'method':'popup','minutes':10},
],
},
};

使用Zapier



Zapier提供了集成Firebase和Google日历的触发器:

I am trying to figure out if it is possible to add events to a user's google calendar server side via Firebase.

I have read this and this which seems to be what I am trying to achieve but it explains that the user which I'd like to add events to their calendar should sharetheir calendar with the account I create for my application.

Is that true or am I misunderstanding something?

I also appreciate it if there is any guide for JavaScript/NodeJS.

解决方案

No. Firebase does not have any built in functionality for adding events to Google calendars. But it's not particularly difficult to connect the two APIs. Below are a couple additional ideas.

Use Functions

One elegant solution would be to use Functions to achieve this by triggering a Functions event by any means (HTTP, a database write, etc) and calling the Calendar API accordingly.

The steps would look something like the following:

  1. When authenticating to Google OAuth on the client, add the calendar scope ('https://www.googleapis.com/auth/calendar')
  2. When triggering the function, send the calendar payload and the Google OAuth token

Inside Cloud Functions for Firebase, your trigger would look something like this:

// 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;
}

And here's some alternative Functions triggers for Realtime Database and 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());
  });

An example eventData object would look something like this:

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},
    ],
  },
};

Use Zapier

Zapier provides a trigger for integrating Firebase and Google Calendar: https://zapier.com/apps/firebase/integrations/google-calendar

这篇关于在应用程序中发生某些事件后,是否可以将事件添加到用户的Google日历中(通过我的服务器)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 00:51
查看更多