forked from christerk/ffbclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fumbblapi.js
92 lines (76 loc) · 2.75 KB
/
fumbblapi.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
var https = require('https');
var fs = require('fs');
var querystring = require('querystring');
var host = "fumbbl.com";
module.exports = {
get_current_matches: function(res){
var matches_options = {
host: host,
path:"/api/match/current",
method:"GET"
};
matches_callback = function(response) {
var str = "";
response.on('data', function (data) {
str += data;
});
response.on('end', function () {
res.json(JSON.parse(str));
});
};
var matches_req = https.request(matches_options, matches_callback);
matches_req.end();
},
authenticate: function(res){
fs.readFile('auth.json', function(err, data) {
if (err) throw err;
var json_data = JSON.parse(data);
var user_id = json_data.user_id;
var form = {
grant_type: "client_credentials",
client_id: json_data.client_id,
client_secret: json_data.secret,
};
var formData = querystring.stringify(form);
var contentLength = formData.length;
var bearer_options = {
host: host,
path:"/api/oauth/token",
method:"POST",
headers:{"Content-Type":"application/x-www-form-urlencoded" }
};
auth_callback = function(response) {
var str = "";
response.on('data', function (data) {
str += data;
});
response.on('end', function () {
res.json({user_id: user_id, token:JSON.parse(str)});
});
};
bearer_callback = function(response) {
var str = "";
response.on('data', function (data) {
str += data;
});
response.on('end', function () {
var bearer = JSON.parse(str);
var auth_token_options = {
host: host,
path:"/api/auth/getToken",
method:"POST",
headers:{
accept: "application/json",
Authorization: " Bearer " + bearer.access_token
}
};
var auth_req = https.request(auth_token_options, auth_callback);
auth_req.end();
});
}
var bearer_req = https.request(bearer_options, bearer_callback);
bearer_req.write(querystring.stringify(form));
bearer_req.end();
});
}
}