2
如何請求訪問用戶的日曆一次,然後查詢事件而無需再次請求訪問?只要沒有被撤銷,是否有訪問密鑰可以授予連續訪問權限?對Google日曆API的持續訪問
我目前在運行express + mongodb的nodejs應用程序中使用google-calendar模塊。
以下示例顯示了創建OAuth請求並重定向迴應用的部分代碼。這很好。我想要的是,只要我有權訪問,就可以隨時查詢用戶的事件。這將用於預訂應用程序,一旦用戶註冊他們的公共Google日曆,就可以讓其他人查看其可用性。
我已經看過Google Calendar API文檔,但我無法弄清楚我需要的正在進行的訪問需要什麼。
配置:
var GoogleCalendar = require('google-calendar');
var google_calendar = new GoogleCalendar.GoogleCalendar(config.google.clientId, config.google.clientSecret, 'http://localhost:3000/authentication');
顯示路線:
app.get('/display', function(req, res) {
// redirect user to Google Authentication
var access_token = req.session.access_token;
if(!access_token) {
req.session.authReturn = req.url;
return res.redirect('/authentication');
}
google_calendar.listCalendarList(access_token, function(err, calendarList) {
// do something with the calendar events...
}
...
身份驗證航線:
app.all('/authentication', function(req, res){
// Redirect the user to Google's authentication form
if(!req.query.code){
google_calendar.getGoogleAuthorizeTokenURL(function(err, redirectUrl) {
if(err) return res.send(500,err);
return res.redirect(redirectUrl);
});
}
// Get access_token from the code
else {
google_calendar.getGoogleAccessToken(req.query, function(err, access_token, refresh_token) {
if(err) return res.send(500,err);
req.session.access_token = access_token;
req.session.refresh_token = refresh_token;
return res.redirect(req.session.authReturn);
});
}
});