forked from OfficeDev/Microsoft-Teams-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
32 lines (29 loc) · 990 Bytes
/
auth.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
var request = require('request');
var graph = require('@microsoft/microsoft-graph-client');
var Q = require('q');
// The auth module object.
var auth = {};
// get application level access token.
auth.getAccessToken = function (tenantId) {
var deferred = Q.defer();
var requestParams = {
grant_type: 'client_credentials',
client_id: process.env.ClientId,
client_secret: process.env.ClientSecret,
scope: 'https://graph.microsoft.com/.default'
};
var url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
request.post({ url: url, form: requestParams }, function (err, response, body) {
var parsedBody = JSON.parse(response.body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error_description);
} else {
// If successful, return the access token.
deferred.resolve(parsedBody.access_token);
}
});
return deferred.promise;
};
module.exports = auth;