-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaleo.js
More file actions
156 lines (146 loc) · 4.14 KB
/
Taleo.js
File metadata and controls
156 lines (146 loc) · 4.14 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Reads the username and password from a Google Doc and returns them as a JSON object.
*
* @return {string} the JSON object containing the username and password
*/
function retreiveCredentials_() {
try {
var doc = DocumentApp.openById('[INSERT-DOC-ID]');
var docText = doc.getText();
}
catch (e) {
throw "Error accessing credentials document. " + e;
return false;
}
var username = Utilities.base64Decode(JSON.parse(docText).username);
var password = Utilities.base64Decode(JSON.parse(docText).password);
if (username == undefined || password == undefined) {
throw "Error parsing credentials."
return false;
}
else {
return JSON.stringify({ 'username' : Utilities.newBlob(username).getDataAsString(), 'password' : Utilities.newBlob(password).getDataAsString() });
}
}
/**
* Authenticates against the Taleo API and returns the authToken needed for subsequent API calls.
*
* @return {string} the authToken needed for subsequent API calls
*/
function login() {
var credentials = retreiveCredentials_();
var loginURL = 'https://ch.tbe.taleo.net/CH06/ats/api/v1/login?orgCode=CITIZENSCHOOLS&userName=' + JSON.parse(credentials).username + '&password=' + JSON.parse(credentials).password;
var loginOptions = {
'method' : 'post',
'contentType' : 'application/json; charset=utf-8'
};
try {
var loginResponse = UrlFetchApp.fetch(loginURL, loginOptions);
if (loginResponse.getResponseCode() == 200) {
Logger.log('Logged in!');
}
else {
throw 'Error logging in: ' + loginResponse.getContentText();
return false;
}
}
catch (e) {
throw 'Could not log in: ' + e;
return false;
}
return authToken = 'authToken=' + JSON.parse(loginResponse).response.authToken;
}
/**
* Uses the passed in authToken to end the current session for the authenticated user.
*
* @param {string} authToken the JSON object containing the username and password
* @return {boolean} returns true if successful, false if unsuccessful
*/
function logout(authToken) {
var logoutURL = 'https://ch.tbe.taleo.net/CH06/ats/api/v1/logout';
var logoutOptions = {
'method' : 'post',
'headers' : {
'cookie' : authToken
},
'contentType' : 'application/json; charset=utf-8'
};
try {
var logoutResponse = UrlFetchApp.fetch(logoutURL, logoutOptions);
if (logoutResponse.getResponseCode() == 200) {
Logger.log('Logged out!');
return true;
}
else {
throw 'Error logging out: ' + logoutResponse.getContentText();
return false;
}
}
catch (e) {
throw 'Could not log out: ' + e;
return false;
}
}
/**
* Makes a Taleo API call.
*
* @param {string} authToken the JSON object containing the username and password
* @param {string} submitURL the URL used for the API call
* @param {string} method the type of API call to make (get, post, put, delete)
* @param {string} content the JSON content submitted in the API call
* @return {boolean} the results of the API submission
*/
function submit(authToken, submitURL, method, content, contentType) {
var submitOptions;
if(content && contentType) {
submitOptions = {
'method' : method,
'headers' : {
'cookie' : authToken
},
'contentType' : contentType,
'payload' : content
};
}
else if(content && (!(contentType))) {
submitOptions = {
'method' : method,
'headers' : {
'cookie' : authToken
},
'payload' : content
};
}
else if((!(content)) && contentType) {
submitOptions = {
'method' : method,
'headers' : {
'cookie' : authToken
},
'contentType' : contentType
};
}
else if((!(content)) && (!(contentType))){
submitOptions = {
'method' : method,
'headers' : {
'cookie' : authToken
}
};
}
try {
var response = UrlFetchApp.fetch(submitURL, submitOptions);
if (response.getResponseCode() == 200) {
Logger.log('Data submitted!');
return response;
}
else {
throw 'Error submitting data: ' + response.getContentText();
return false;
}
}
catch (e) {
throw 'Could not submit data: ' + e;
return false;
}
}