forked from microsoftgraph/msgraph-sample-nodeexpressapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
123 lines (109 loc) · 3.42 KB
/
graph.js
File metadata and controls
123 lines (109 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var graph = require('@microsoft/microsoft-graph-client');
require('isomorphic-fetch');
module.exports = {
getUserDetails: async function(msalClient, userId) {
const client = getAuthenticatedClient(msalClient, userId);
const user = await client
.api('/me')
.select('displayName,mail,mailboxSettings,userPrincipalName')
.get();
return user;
},
// <GetCalendarViewSnippet>
getCalendarView: async function(msalClient, userId, start, end, timeZone) {
const client = getAuthenticatedClient(msalClient, userId);
return client
.api('/me/calendarview')
// Add Prefer header to get back times in user's timezone
.header('Prefer', `outlook.timezone="${timeZone}"`)
// Add the begin and end of the calendar window
.query({
startDateTime: encodeURIComponent(start),
endDateTime: encodeURIComponent(end)
})
// Get just the properties used by the app
.select('subject,organizer,start,end')
// Order by start time
.orderby('start/dateTime')
// Get at most 50 results
.top(50)
.get();
},
// </GetCalendarViewSnippet>
// <CreateEventSnippet>
createEvent: async function(msalClient, userId, formData, timeZone) {
const client = getAuthenticatedClient(msalClient, userId);
// Build a Graph event
const newEvent = {
subject: formData.subject,
start: {
dateTime: formData.start,
timeZone: timeZone
},
end: {
dateTime: formData.end,
timeZone: timeZone
},
body: {
contentType: 'text',
content: formData.body
}
};
// Add attendees if present
if (formData.attendees) {
newEvent.attendees = [];
formData.attendees.forEach(attendee => {
newEvent.attendees.push({
type: 'required',
emailAddress: {
address: attendee
}
});
});
}
// POST /me/events
await client
.api('/me/events')
.post(newEvent);
},
// </CreateEventSnippet>
};
function getAuthenticatedClient(msalClient, userId) {
if (!msalClient || !userId) {
throw new Error(
`Invalid MSAL state. Client: ${msalClient ? 'present' : 'missing'}, User ID: ${userId ? 'present' : 'missing'}`);
}
// Initialize Graph client
const client = graph.Client.init({
// Implement an auth provider that gets a token
// from the app's MSAL instance
authProvider: async (done) => {
try {
// Get the user's account
const account = await msalClient
.getTokenCache()
.getAccountByHomeId(userId);
if (account) {
// Attempt to get the token silently
// This method uses the token cache and
// refreshes expired tokens as needed
const scopes = process.env.OAUTH_SCOPES || 'https://graph.microsoft.com/.default';
const response = await msalClient.acquireTokenSilent({
scopes: scopes.split(','),
redirectUri: process.env.OAUTH_REDIRECT_URI,
account: account
});
// First param to callback is the error,
// Set to null in success case
done(null, response.accessToken);
}
} catch (err) {
console.log(JSON.stringify(err, Object.getOwnPropertyNames(err)));
done(err, null);
}
}
});
return client;
}