-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequest.js
More file actions
83 lines (74 loc) · 2.69 KB
/
request.js
File metadata and controls
83 lines (74 loc) · 2.69 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
var api_url = "http://chaira.udla.edu.co/api/v0.1";
function requestAccessToken(code, callback) {
var http = new XMLHttpRequest();
var params = {
grant_type: "authorization_code",
code: code,
redirect_uri: "http://localhost:3000",
client_id: "919207462918",
client_secret: "l31dj7e645jww7po9xe42gymr0oxkn",
state: "xyz"
};
http.open('POST', api_url + '/oauth2/authorize.asmx/token', true);
http.setRequestHeader("Content-type", "application/json");
http.send(JSON.stringify(params));
http.onreadystatechange = function(e) {
if (http.readyState == 4) {
if (http.status == 200) {
var res = JSON.parse(this.response.replace('{"d":null}', ''));
if (res.state == "xyz") {
callback(res);
} else if (res.type == "invalid_grant") {
if (localStorage.getItem("token") != null) {
callback("token_exist");
} else {
getError('El codigo es invalido.');
}
}
}
}
};
}
function requestResource(scope, callback) {
var http = new XMLHttpRequest();
var params = {
access_token: localStorage.getItem("token"),
scope: scope
};
http.open('POST', api_url + '/oauth2/resource.asmx/scope', true);
http.setRequestHeader("Content-type", "application/json");
http.send(JSON.stringify(params));
http.onreadystatechange = function(e) {
if (http.readyState == 4) {
if (http.status == 200) {
var res = JSON.parse(this.response.replace('{"d":null}', ''));
if (res.state == "OK") {
callback(res);
} else if (res.type == "invalid_scope") {
getErrorInformation("La aplicación no tiene permisos necesarios para esta información.");
} else {
getErrorInformation(res.description);
}
}
}
};
}
function requestLogout(callback) {
var http = new XMLHttpRequest();
var params = {
access_token: localStorage.getItem("token")
};
http.open('POST', api_url + '/oauth2/resource.asmx/logout', true);
http.setRequestHeader("Content-type", "application/json");
http.send(JSON.stringify(params));
http.onreadystatechange = function(e) {
if (http.readyState == 4) {
if (http.status == 200) {
var res = JSON.parse(this.response.replace('{"d":null}', ''));
if (res.state == "OK") {
callback(res);
}
}
}
};
}