Google Ionic 中的日历
Google calendar in Ionic
我正在尝试使用 Ionic 创建一个移动应用程序,对于这个应用程序,我们需要使用 Google 日历。 objective 是为了创建一个应用程序,某些人可以在其中订阅某些事件。这些事件现在已在 Google 日历中创建,我们希望能够在我们的 Google 日历或仅显示某些事件的应用程序中订阅自己。
我已经找了很久了,但我就是不知道如何在 Ionic 中连接 Google 日历。
请帮助我开始。
我已经能够自己解决,但不能直接在 Ionic 中解决。
我联系了我的 node.js 后端,在 node.js 中可以联系 google api...
let eventCategories = require('./event.model');
let fs = require('fs');
let readline = require('readline');
let google = require('googleapis');
let googleAuth = require('google-auth-library');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-nodejs-quickstart.json
let SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
let TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
let TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';
module.exports = {
getEventCategories,
getEvents
};
function getEventCategories(){
return eventCategories.find({});
}
function getEvents(calendarId) {
return loadClientSecrets()
.then(res => authorize(JSON.parse(res))
.then(response => listEvents(response, calendarId)))
}
function loadClientSecrets(){
return new Promise(function (fulfill, reject){
fs.readFile('client_secret.json', function processClientSecrets(err, content){
if (err){
console.log('Error loading client secret file: '+ err);
reject(err);
}
else fulfill(content);
})
})
}
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*/
function authorize(credentials) {
return new Promise(function (fulfill, reject){
let clientSecret = credentials.installed.client_secret;
let clientId = credentials.installed.client_id;
let redirectUrl = credentials.installed.redirect_uris[0];
let auth = new googleAuth();
let oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
fulfill(getNewToken(oauth2Client));
} else {
oauth2Client.credentials = JSON.parse(token);
fulfill(oauth2Client);
}
});
})
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*/
function getNewToken(oauth2Client) {
new Promise (function (fulfill, reject){
let authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
reject(err);
}
oauth2Client.credentials = token;
storeToken(token);
fulfill(oauth2Client);
});
});
})
}
/**
* Store token to disk be used in later program executions.
*/
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
/**
* Lists the next 10 events on the user's primary calendar.
*/
function listEvents(auth, calendarId) {
return new Promise(function ( fulfill, reject){
var calendar = google.calendar('v3');
calendar.events.list({
auth: auth,
calendarId: calendarId,
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
reject(err);
}
let events = response.items;
if (events.length == 0) {
console.log('No upcoming events found.');
} else {
console.log('Upcoming 10 events:');
for (let i = 0; i < events.length; i++) {
let event = events[i];
let start = event.start.dateTime || event.start.date;
console.log('%s - %s', start, event.summary);
}
fulfill(events)
}
});
})
}
我正在尝试使用 Ionic 创建一个移动应用程序,对于这个应用程序,我们需要使用 Google 日历。 objective 是为了创建一个应用程序,某些人可以在其中订阅某些事件。这些事件现在已在 Google 日历中创建,我们希望能够在我们的 Google 日历或仅显示某些事件的应用程序中订阅自己。
我已经找了很久了,但我就是不知道如何在 Ionic 中连接 Google 日历。
请帮助我开始。
我已经能够自己解决,但不能直接在 Ionic 中解决。
我联系了我的 node.js 后端,在 node.js 中可以联系 google api...
let eventCategories = require('./event.model');
let fs = require('fs');
let readline = require('readline');
let google = require('googleapis');
let googleAuth = require('google-auth-library');
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/calendar-nodejs-quickstart.json
let SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'];
let TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
let TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json';
module.exports = {
getEventCategories,
getEvents
};
function getEventCategories(){
return eventCategories.find({});
}
function getEvents(calendarId) {
return loadClientSecrets()
.then(res => authorize(JSON.parse(res))
.then(response => listEvents(response, calendarId)))
}
function loadClientSecrets(){
return new Promise(function (fulfill, reject){
fs.readFile('client_secret.json', function processClientSecrets(err, content){
if (err){
console.log('Error loading client secret file: '+ err);
reject(err);
}
else fulfill(content);
})
})
}
/**
* Create an OAuth2 client with the given credentials, and then execute the
* given callback function.
*/
function authorize(credentials) {
return new Promise(function (fulfill, reject){
let clientSecret = credentials.installed.client_secret;
let clientId = credentials.installed.client_id;
let redirectUrl = credentials.installed.redirect_uris[0];
let auth = new googleAuth();
let oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
fulfill(getNewToken(oauth2Client));
} else {
oauth2Client.credentials = JSON.parse(token);
fulfill(oauth2Client);
}
});
})
}
/**
* Get and store new token after prompting for user authorization, and then
* execute the given callback with the authorized OAuth2 client.
*/
function getNewToken(oauth2Client) {
new Promise (function (fulfill, reject){
let authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
reject(err);
}
oauth2Client.credentials = token;
storeToken(token);
fulfill(oauth2Client);
});
});
})
}
/**
* Store token to disk be used in later program executions.
*/
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
/**
* Lists the next 10 events on the user's primary calendar.
*/
function listEvents(auth, calendarId) {
return new Promise(function ( fulfill, reject){
var calendar = google.calendar('v3');
calendar.events.list({
auth: auth,
calendarId: calendarId,
timeMin: (new Date()).toISOString(),
maxResults: 10,
singleEvents: true,
orderBy: 'startTime'
}, function(err, response) {
if (err) {
console.log('The API returned an error: ' + err);
reject(err);
}
let events = response.items;
if (events.length == 0) {
console.log('No upcoming events found.');
} else {
console.log('Upcoming 10 events:');
for (let i = 0; i < events.length; i++) {
let event = events[i];
let start = event.start.dateTime || event.start.date;
console.log('%s - %s', start, event.summary);
}
fulfill(events)
}
});
})
}