forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode-sample.js
285 lines (239 loc) · 6.24 KB
/
node-sample.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// when using the npm module, use the following syntax
// var MicrosoftGraph = require("@microsoft/microsoft-graph-client").Client;
// for fast development, simply require the generated lib without bundling the npm module
const MicrosoftGraph = require("../../lib/src/index.js").Client;
const secrets = require("./secrets");
const client = MicrosoftGraph.init({
defaultVersion: 'v1.0',
debugLogging: true,
authProvider: (done) => {
done(null, secrets.accessToken);
}
});
/*
// Get the name of the authenticated user with callbacks
client
.api('/me')
.select("displayName")
.get((err, res) => {
if (err) {
console.log(err)
return;
}
console.log(res.displayName);
});
// Get the name of the authenticated user with promises
client
.api('/me')
.select("displayName")
.get()
.then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
});
*/
// Update the authenticated users birthday.
client
.api('/me')
.header("content-type", "application/json")
.update(
{ "birthday": "1908-12-22T00:00:00Z" },
(err, res) => {
if (err) {
console.log(err);
return;
}
console.log("Updated my birthday");
}
);
/*
// GET /users
client
.api('/users')
.version('beta')
.get((err, res) => {
if (err) {
console.log(err);
return;
}
console.log("Found", res.value.length, "users");
});
// Find my top 5 contacts on the beta endpoint
client
.api('/me/people')
.version('beta')
.top(5)
.select("displayName")
.get((err, res) => {
if (err) {
console.
console.log("%c" + err, 'color: #bada55')
return;
}
const topContacts = res.value.map((u) => { return u.displayName });
console.log("Your top contacts are", topContacts.join(", "));
});
// Use promises instead of callbacks
// .get() returns a Promise
client
.api('/me')
.select("displayName")
.get()
.then((res) => {
console.log(res.displayName);
})
.catch(console.error);
// Find my top 5 contacts on the beta endpoint
// .select() can be called multiple times
client
.api('/me/people')
.top(5)
.version('beta')
.select("displayName")
.select("title") // or call with .select(["displayName", "title"]) or .select("displayName", "title")
.get((err, res) => {
if (err) {
console.log(err)
return;
}
console.log(res.value[0].title, res.value[0].displayName);
});
// send an email
const mail = {
subject: "MicrosoftGraph JavaScript SDK Samples",
toRecipients: [{
emailAddress: {
address: "[email protected]"
}
}],
body: {
content: "<h1>MicrosoftGraph TypeScript Connect Sample</h1><br>https://github.com/microsoftgraph/msgraph-sdk-javascript",
contentType: "html"
}
}
client
.api('/users/me/sendMail')
.post(
{ message: mail },
(err, res) => {
if (err)
console.log(err);
else
console.log("Sent an email");
})
// GET 3 of my events
client
.api('/me/events')
.top(3)
.get((err, res) => {
if (err) {
console.log(err)
return;
}
var upcomingEventNames = []
for (var i = 0; i < res.value.length; i++) {
upcomingEventNames.push(res.value[i].subject);
}
console.log("My calendar events include", upcomingEventNames.join(", "))
})
*/
// URL substitution example
// let userIds = [secrets.userId1,
// secrets.userId2];
// for (let i = 0; i < userIds.length; i++) {
// let fetchUser = client
// .api(`/me/people/${userIds[i]}`)
// .version('beta')
// .select('displayName')
// .get((err, res) => {
// if (err) {
// console.log(err)
// return;
// }
// console.log(res.displayName)
// })
// }
/*
// Find my top 5 contacts
client
.api('/me/people')
.version('beta')
.top(5)
.select("displayName")
.select("id")
.get((err, res) => {
console.log(res)
});
client
.api("/users")
.filter("startswith(displayName, 'david')")
.get((err, res) => {
if (err) {
console.log(err);
return;
}
console.log(res)
})
// custom header example
client
.api('/me')
.select("displayName")
.header('foo1', 'bar1')
.headers({ 'foo2': 'bar2' }) //.headers() for object, .header() for 2 params
.headers({ 'foo3': 'bar3', 'foo4': 'bar4' })
.get((err, res) => {
if (err) {
console.log(err)
return;
}
});
// delete a OneDrive item
client
.api(`/me/drive/items/${secrets.ONE_DRIVE_FILE_ID_TO_DELETE}`)
.delete((err, res) => {
if (err) {
console.log(err)
return;
}
console.log(res)
})
*/
/*
// Download a file from OneDrive
let fs = require('fs'); // requires filesystem module
client
.api('/me/drive/root/children/Book.xlsx/content')
.getStream((err, downloadStream) => {
if (err) {
console.log(err);
return;
}
let writeStream = fs.createWriteStream('../Book1.xlsx');
downloadStream.pipe(writeStream).on('error', console.log);
});
// Upload a file to OneDrive
let photoReadStream = fs.createReadStream('../logo.png');
client
.api('/me/drive/root/children/logo234.png/content')
.putStream(photoReadStream, (err) => {
console.log(err);
});
// Download my photo
client
.api('https://graph.microsoft.com/v1.0/me/photo/$value')
.getStream((err, downloadStream) => {
let writeStream = fs.createWriteStream('../myPhoto.jpg');
downloadStream.pipe(writeStream).on('error', console.log);
});
// Update my photo
let profilePhotoReadStream = fs.createReadStream('me.jpg');
client
.api('/me/photo/$value')
.putStream(profilePhotoReadStream, (err) => {
if (err) {
console.log(err);
return;
}
});
*/