Commit a54f2679add31757758547e886eff333d24a92d7
1 parent
f6a4415a
Exists in
master
fix API
Showing
4 changed files
with
70 additions
and
62 deletions
Show diff stats
app/controllers/home.controller.js
... | ... | @@ -51,14 +51,14 @@ function create(req, res) { |
51 | 51 | let endDate = payload.endDate |
52 | 52 | |
53 | 53 | lib.authorize((err, auth) => { |
54 | + let options = lib.eventBuilder(payload) | |
54 | 55 | if (err) { |
55 | 56 | res.send(err) |
57 | + } else { | |
58 | + options.auth = auth | |
56 | 59 | } |
57 | 60 | |
58 | - // let options = lib.eventBuilder(payload) | |
59 | - // options.auth = auth | |
60 | - | |
61 | - lib.createEvent(auth, payload, (err, result) => { | |
61 | + lib.createEvent(options, (err, result) => { | |
62 | 62 | if (err) { |
63 | 63 | res.send(err) |
64 | 64 | } else { | ... | ... |
app/lib/index.js
... | ... | @@ -3,22 +3,21 @@ |
3 | 3 | const google = require('googleapis'); |
4 | 4 | const googleAuth = require('google-auth-library'); |
5 | 5 | const calendar = google.calendar('v3'); |
6 | - | |
7 | 6 | const fs = require('fs'); |
7 | +const path = require('path'); | |
8 | +const yamlConfig = require('node-yaml-config'); | |
9 | +const config = yamlConfig.load(path.join(__dirname, '/../../config/config.yml')); | |
10 | +const CALENDAR_ID = config.calendarID | |
8 | 11 | |
9 | -const SCOPES = [process.env.SCOPES]; | |
12 | +const SCOPES = ['https://www.googleapis.com/auth/calendar']; | |
10 | 13 | const TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; |
11 | 14 | const TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; |
12 | 15 | |
13 | 16 | module.exports = { |
14 | - | |
15 | 17 | authorize: (callback) => { |
16 | 18 | |
17 | - fs.readFile('client_secret.json', function processClientSecrets(err, content) { | |
18 | - if (err) { | |
19 | - console.log('Error loading client secret file: ' + err); | |
20 | - return; | |
21 | - } | |
19 | + fs.readFile('client_secret.json', (err, content) => { | |
20 | + if (err) return callback(err); | |
22 | 21 | |
23 | 22 | let credentials = JSON.parse(content); |
24 | 23 | var clientSecret = credentials.installed.client_secret; |
... | ... | @@ -27,71 +26,80 @@ module.exports = { |
27 | 26 | var auth = new googleAuth(); |
28 | 27 | var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); |
29 | 28 | |
30 | - // Check if we have previously stored a token. | |
31 | - fs.readFile(TOKEN_PATH, function (err, token) { | |
32 | - if (err) { | |
33 | - return callback(err); | |
34 | - } else { | |
35 | - oauth2Client.credentials = JSON.parse(token); | |
36 | - return callback(null, oauth2Client); | |
37 | - } | |
38 | - }); | |
39 | - | |
40 | - }); | |
41 | - | |
42 | - }, | |
29 | + fs.readFile(TOKEN_PATH, (err, token) => { | |
30 | + if (err) return callback(err); | |
43 | 31 | |
44 | - getNewToken: (oauth2Client, callback) => { | |
45 | - var authUrl = oauth2Client.generateAuthUrl({ | |
46 | - access_type: 'offline', | |
47 | - scope: SCOPES | |
48 | - }); | |
49 | - console.log('Authorize this app by visiting this url: ', authUrl); | |
50 | - var rl = readline.createInterface({ | |
51 | - input: process.stdin, | |
52 | - output: process.stdout | |
53 | - }); | |
54 | - rl.question('Enter the code from that page here: ', function (code) { | |
55 | - rl.close(); | |
56 | - oauth2Client.getToken(code, function (err, token) { | |
57 | - if (err) { | |
58 | - console.log('Error while trying to retrieve access token', err); | |
59 | - return; | |
60 | - } | |
61 | - oauth2Client.credentials = token; | |
62 | - storeToken(token); | |
63 | - callback(oauth2Client); | |
32 | + oauth2Client.credentials = JSON.parse(token); | |
33 | + return callback(null, oauth2Client); | |
64 | 34 | }); |
65 | 35 | }); |
36 | + | |
66 | 37 | }, |
67 | 38 | |
68 | 39 | listEvents: (auth, callback) => { |
69 | 40 | calendar.events.list({ |
70 | 41 | auth: auth, |
71 | - calendarId: process.env.CALENDAR_ID, | |
42 | + calendarId: CALENDAR_ID || 'primary', | |
72 | 43 | timeMin: (new Date()).toISOString(), |
73 | 44 | maxResults: 50, |
74 | 45 | singleEvents: true, |
75 | 46 | orderBy: 'startTime' |
76 | 47 | }, (err, response) => { |
77 | - if (err) { | |
78 | - return callback(err); | |
79 | - } | |
48 | + if (err) return callback(err); | |
49 | + | |
80 | 50 | return callback(null, response); |
81 | 51 | }); |
82 | 52 | }, |
83 | 53 | |
84 | - createEvent: (auth, event, callback) => { | |
85 | - | |
54 | + createEvent: (options, callback) => { | |
86 | 55 | calendar.events.insert({ |
87 | - auth: auth, | |
88 | - calendarId: process.env.CALENDAR_ID, | |
89 | - resource: event, | |
90 | - }, (err, event) => { | |
91 | - if (err) { | |
92 | - return callback('There was an error contacting the Calendar service: ' + err); | |
93 | - } | |
94 | - return callback(null, event.htmlLink); | |
56 | + auth: options.auth, | |
57 | + calendarId: CALENDAR_ID || 'primary', | |
58 | + resource: options | |
59 | + }, (err, response) => { | |
60 | + if (err) return callback(err); | |
61 | + | |
62 | + return callback(null, response); | |
63 | + }); | |
64 | + }, | |
65 | + | |
66 | + deleteEvent: (options, callback) => { | |
67 | + calendar.events.delete({ | |
68 | + auth: options.auth, | |
69 | + calendarId: CALENDAR_ID || 'primary', | |
70 | + eventId: options.eventId | |
71 | + }, (err, response) => { | |
72 | + if (err) return callback(err); | |
73 | + | |
74 | + return callback(null, response); | |
95 | 75 | }); |
76 | + }, | |
77 | + | |
78 | + eventBuilder: (payload) => { | |
79 | + | |
80 | + return { | |
81 | + summary: payload.summary, | |
82 | + description: payload.description, | |
83 | + start: { | |
84 | + dateTime: payload.startDate, | |
85 | + timeZone: 'Asia/Bangkok' | |
86 | + }, | |
87 | + end: { | |
88 | + dateTime: payload.endDate, | |
89 | + timeZone: 'Asia/Bangkok' | |
90 | + }, | |
91 | + attendees: [ | |
92 | + { email: payload.email } | |
93 | + ], | |
94 | + reminders: { | |
95 | + useDefault: false, | |
96 | + overrides: [ | |
97 | + { | |
98 | + method: 'email', | |
99 | + minutes: 24 * 60 | |
100 | + } | |
101 | + ] | |
102 | + } | |
103 | + } | |
96 | 104 | } |
97 | 105 | } |
98 | 106 | \ No newline at end of file | ... | ... |
config/config.yml
1 | 1 | ##### deverlopment Config |
2 | 2 | localhost: |
3 | - skipLogin: true | |
4 | 3 | debug: true |
5 | 4 | server: |
6 | 5 | url: 'localhost' |
... | ... | @@ -15,4 +14,5 @@ localhost: |
15 | 14 | user: |
16 | 15 | pass: |
17 | 16 | timerecheck: '10000' #millisecond |
18 | - apitimeout: 3000 #millisecond | |
19 | 17 | \ No newline at end of file |
18 | + apitimeout: 3000 #millisecond | |
19 | + calendarID: 'rvmbg3kg7uqninf7n3au1ku4mc@group.calendar.google.com' | |
20 | 20 | \ No newline at end of file | ... | ... |
quickstart.js
... | ... | @@ -7,7 +7,7 @@ var googleAuth = require('google-auth-library'); |
7 | 7 | |
8 | 8 | // If modifying these scopes, delete your previously saved credentials |
9 | 9 | // at ~/.credentials/calendar-nodejs-quickstart.json |
10 | -var SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']; | |
10 | +var SCOPES = ['https://www.googleapis.com/auth/calendar']; | |
11 | 11 | var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || |
12 | 12 | process.env.USERPROFILE) + '/.credentials/'; |
13 | 13 | var TOKEN_PATH = TOKEN_DIR + 'calendar-nodejs-quickstart.json'; | ... | ... |