The*_*Ben 2 google-calendar-api google-api firebase google-cloud-functions
我想弄清楚是否可以通过 Firebase 将事件添加到用户的谷歌日历服务器端。
我已经阅读了这个和这个,这似乎是我想要实现的目标,但它解释了我想向他们的日历添加事件的用户应该与我为我的应用程序创建的帐户共享他们的日历。
这是真的还是我误解了什么?
如果有任何关于 JavaScript/NodeJS 的指南,我也很感激。
不可以。Firebase 没有任何用于向 Google 日历添加事件的内置功能。但是连接这两个 API 并不是特别困难。下面是一些额外的想法。
使用函数
一种优雅的解决方案是使用函数来实现这一点,方法是通过任何方式(HTTP、数据库写入等)触发函数事件并相应地调用日历 API。
步骤如下所示:
在 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;
}
Run Code Online (Sandbox Code Playgroud)
这是实时数据库和 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());
});
Run Code Online (Sandbox Code Playgroud)
一个示例 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},
],
},
};
Run Code Online (Sandbox Code Playgroud)
使用 Zapier
Zapier 提供了集成 Firebase 和 Google 日历的触发器:https ://zapier.com/apps/firebase/integrations/google-calendar
| 归档时间: |
|
| 查看次数: |
3418 次 |
| 最近记录: |