-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequestUtil.js
More file actions
executable file
·107 lines (100 loc) · 3.18 KB
/
requestUtil.js
File metadata and controls
executable file
·107 lines (100 loc) · 3.18 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
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
var https = require('https');
/**
* Generates a GET request the user endpoint
* @param {string} accessToken the access token with which the request should be authenticated
* @param {callback} callback
*/
function getUserData(accessToken, callback) {
var options = {
host: 'graph.microsoft.com',
path: '/v1.0/me',
method: 'GET',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer ' + accessToken
}
};
https.get(options, function (response) {
var body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
var error;
if (response.statusCode === 200) {
callback(null, JSON.parse(body));
} else {
error = new Error();
error.code = response.statusCode;
error.message = response.statusMessage;
// The error body sometimes includes an empty space
// before the first character, remove it or it causes an error.
body = body.trim();
error.innerError = JSON.parse(body).error;
callback(error, null);
}
});
}).on('error', function (e) {
callback(e, null);
});
}
/**
* Generates a POST request to the SendMail endpoint
* @param {string} accessToken the access token with which the request should be authenticated
* @param {string} data the data which will be 'POST'ed
* @param {callback} callback
*/
function postSendMail(accessToken, mailBody, callback) {
var outHeaders = {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + accessToken,
'Content-Length': mailBody.length
};
var options = {
host: 'graph.microsoft.com',
path: '/v1.0/me/microsoft.graph.sendMail',
method: 'POST',
headers: outHeaders
};
// Set up the request
var post = https.request(options, function (response) {
var body = '';
response.on('data', function (d) {
body += d;
});
response.on('end', function () {
var error;
if (response.statusCode === 202) {
callback(null);
} else {
error = new Error();
error.code = response.statusCode;
error.message = response.statusMessage;
// The error body sometimes includes an empty space
// before the first character, remove it or it causes an error.
body = body.trim();
error.innerError = JSON.parse(body).error;
// Note: If you receive a 500 - Internal Server Error
// while using a Microsoft account (outlok.com, hotmail.com or live.com),
// it's possible that your account has not been migrated to support this flow.
// Check the inner error object for code 'ErrorInternalServerTransientError'.
// You can try using a newly created Microsoft account or contact support.
callback(error);
}
});
});
// write the outbound data to it
post.write(mailBody);
// we're done!
post.end();
post.on('error', function (e) {
callback(e);
});
}
exports.getUserData = getUserData;
exports.postSendMail = postSendMail;