-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth-module-with-promise.js
58 lines (34 loc) · 1.17 KB
/
oauth-module-with-promise.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
const request = require('request');
var get_oauth_token = (env) => {
return new Promise((resolve,reject)=> {
var headers = {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded'
}
var options = {
url: `${env.oauth.url}/api/oauth/token`,
method: 'POST',
headers: headers,
json: true,
form: {
'client_id': env.oauth.client_id,
'client_secret': env.oauth.client_secret,
'grant_type': 'client_credentials',
'scope': 'registration'
}
}
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
resolve({
access_token: body.access_token,
expires_in: body.expires_in,
scope: body.scope,
token_type: body.token_type
});
}
else
reject('Unable to connect to the server')
});
});
};
module.exports.get_oauth_token = get_oauth_token;