-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathSampleRequests.js
146 lines (136 loc) · 3.52 KB
/
SampleRequests.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
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
// First, create an instance of the Microsoft Graph JS SDK Client class
const { client } = require("../clientInitialization/ClientWithOptions");
/**
* OR
* const { client } = require("../clientInitialization/TokenCredentialAuthenticationProvider");
* OR
* require or import client created using an custom authentication provider
*/
// 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"
})
.then((res) => {
console.log("Updated my birthday");
})
.catch((err) => {
console.log(err);
});
// GET /users
client
.api('/users')
.version('beta')
.get()
.then((res) => {
console.log("Found", res.value.length, "users");
})
.catch((err) => {
console.log(err);
});
// Find my top 5 contacts on the beta endpoint
client
.api('/me/people')
.version('beta')
.top(5)
.select("displayName")
.get()
.then((res) => {
const topContacts = res.value.map((u) => {
return u.displayName
});
console.log("Your top contacts are", topContacts.join(", "));
})
.catch((err) => {
console.log(err);
});
// send an email
const mail = {
subject: "MicrosoftGraph JavaScript SDK Samples",
toRecipients: [{
emailAddress: {
address: "<TO_EMAIL_ADDRESS>"
}
}],
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
})
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
// GET 3 of my events
client
.api('/me/events')
.top(3)
.get()
.then((res) => {
let upcomingEventNames = []
for (let i = 0; i < res.value.length; i++) {
upcomingEventNames.push(res.value[i].subject);
}
console.log("My calendar events include", upcomingEventNames.join(", "))
})
.catch((err) => {
console.log(err);
});
// Download a file from OneDrive
client
.api('/me/drive/items/<ITEM_ID>/content')
.getStream()
.then((stream) => {
let writeStream = fs.createWriteStream(`<FILE_NAME_WITH_EXTENSION>`);
stream.pipe(writeStream).on('error', err => {
console.log(err);
});
writeStream.on('finish', () => {
console.log("finish");
});
writeStream.on('error', err => {
console.log(err);
});
})
.catch((err) => {
console.log(err);
});
// Upload a file to OneDrive
let readStream = fs.createReadStream("<FILE_PATH>");
client
.api('/me/drive/root/children/<FILE_NAME_WITH_EXTENSION>/content')
.putStream(readStream)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
*/