-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
49 lines (42 loc) · 1.24 KB
/
lib.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
var request = require('request');
var querystring = require('querystring');
var apiBaseUrl = 'https://api.singly.com';
module.exports = function(clientId, clientSecret, redirectURI) {
var client = {};
client.getAuthorizeURL = function(service) {
return apiBaseUrl + '/oauth/authorize?' + querystring.stringify({
client_id: clientId,
redirect_uri: redirectURI,
service: service
});
}
client.getAccessToken = function(code, callback) {
var data = {
client_id: clientId,
client_secret: clientSecret,
code: code
};
request.post({
uri: apiBaseUrl + '/oauth/access_token',
body: querystring.stringify(data),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, function (err, resp, body) {
if(err) return callback(err, body);
try {
body = JSON.parse(body);
callback(undefined, body.access_token);
} catch(parseErr) {
callback(err, body);
}
});
}
client.apiCall = function(path, params, callback) {
var uri = apiBaseUrl + path + '?' + querystring.stringify(params);
request.get({uri:uri, json:true}, function(err, resp, json) {
callback(err, json);
});
}
return client;
}