· 5 years ago · Sep 12, 2020, 02:02 PM
1<!DOCTYPE html>
2<html lang=en>
3<head>
4 <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
5 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
6</head>
7<body>
8 <div id="app">
9 <div><span>Client ID :</span> {{ CLIENT_ID || 'Non défini' }} </div>
10 <div><span>Client Secret :</span> {{ CLIENT_SECRET || 'Non défini' }}</div>
11 <div v-if="!query || inError" style="font-weight: bold; font-size: 14px;"><br /> Redirection vers l'authentification ... </div>
12 <template v-else-if="query && query.code && !inError">
13 <div><span>Client Code </span> {{ query.code || 'Non défini' }} </div>
14 <div><span>Client Authorization :</span> {{ Authorization || 'Non défini' }}</div><br /><br />
15 <div><span>Account KEY: </span> {{ account_key || 'Non défini' }} </div>
16 <div><span>Organizer KEY: </span> {{ organizer_key || 'Non défini' }} </div><br/><br/>
17 <div><span>Access Token :</span> {{ access_token || 'Non défini' }}</div>
18 <div><span>Refresh Token :</span> {{ refresh_token || 'Non défini' }} </div>
19 <br /><br />
20 <div v-if="users.length">
21 <span>USERS LIST : </span>
22 <table>
23 <tr><th>Email</th><th>Identity</th><th>LicenceKey</th></tr>
24 <tr
25 v-for="(user, index) in users"
26 :key="'user'+index"
27 >
28 <td>{{ user.email }}</td>
29 <td>{{ user.firstName }} {{ user.lastName }}</td>
30 <td> {{ user.licenseKeys }}</td>
31 </tr>
32 </table>
33 </div>
34 <br /><br />
35 <div v-if="trainings.length">
36 <span>AVAILABLE TRAININGS : </span>
37 <table>
38 <tr><th>ID</th><th>KEY</th><th>Date</th><th>Name</th><th>Desc</th><th>Organizers</th></tr>
39 <template v-for="(training, index) in trainings">
40 <tr
41 :key="'trainings'+index"
42 >
43 <td>{{ training.trainingId }}</td>
44 <td>{{ training.trainingKey }}</td>
45 <td>{{ training.times[0].startDate }} <br /> {{ training.times[0].endDate }} </td>
46 <td>{{ training.name }}</td>
47 <td>{{ training.description }}</td>
48 <td>{{ training.organizers[0].email }} </td>
49 </tr>
50 <tr v-if="training.participants">
51 {{ !training.participants || training.participants.length === 0 ? 'Pas de participants' : 'Participants :' + training.participants }}
52 </tr>
53 </template>
54 </table>
55 </div>
56 </template>
57 </div>
58 </body>
59</html>
60
61 <script type="text/javascript">
62 const URL = 'https://faed3172dba0.ngrok.io/'; // <-------------- A MODIFIER SELON l'URL DE CALLBACK CONFIGURER
63 const API_AUTH = 'https://api.getgo.com/oauth/v2/';
64 const API_USERS = 'https://api.getgo.com/admin/rest/v1/';
65 const API_GTT = 'https://api.getgo.com/G2T/rest/';
66 const CLIENT_ID = '6161baeb-a4ec-48c8-b9b8-32d80d8a990b';
67 const CLIENT_SECRET = 'R/o4ahT7q2HRXyqusp+nMA==';
68 const OAUTH_URL = `${API_AUTH}authorize?client_id=${CLIENT_ID}&response_type=code&redirect_uri=${URL}`;
69
70 var app = new Vue({
71 el: '#app',
72 data: {
73 CLIENT_ID, CLIENT_SECRET,
74 inError: false,
75 query: null,
76 access_token: null,
77 refresh_token: null,
78 next_token: null,
79 account_key: null,
80 organizer_key: null,
81 users: [],
82 trainings: [],
83 training: null,
84 },
85 created()
86 {
87 let uri = window.location.href.split('?');
88 if (uri.length == 2)
89 {
90 let vars = uri[1].split('&');
91 let getVars = {};
92 let tmp = '';
93 vars.forEach(function(v){
94 tmp = v.split('=');
95 if(tmp.length == 2)
96 getVars[tmp[0]] = tmp[1];
97 });
98 this.query = getVars;
99 }
100 },
101 async mounted() {
102 if (this.query && this.query.code) {
103 if (await this.getBaseToken()) {
104 await this.getUsers();
105 await this.getTrainings();
106 await this.getTrainingsRegistrants();
107 } else { this.goAuthPage() }
108 } else {
109 this.goAuthPage();
110 }
111 },
112 computed: {
113 Authorization() {
114 return btoa(CLIENT_ID+':'+CLIENT_SECRET);
115 },
116 },
117 methods: {
118 isCorrectRequest(status) {
119 return [202,200].indexOf(status) > -1;
120 },
121 goAuthPage() {
122 window.location = OAUTH_URL;
123 },
124 async getBaseToken() {
125 const request = await axios.post(`${API_AUTH}token?redirect_uri=${URL}&grant_type=authorization_code&code=${this.query.code}`, {}, {
126 headers: {
127 'Content-Type': 'application/x-www-form-urlencoded',
128 Authorization: `Basic ${this.Authorization}`,
129 Accept: 'application/json',
130 },
131 }).catch((e) => {
132 this.inError = true;
133 return false;
134 })
135 if (this.isCorrectRequest(request.status)) {
136 this.access_token = request.data.access_token || null;
137 this.refresh_token = request.data.refresh_token || null;
138 this.account_key = request.data.account_key || null;
139 this.organizer_key = request.data.organizer_key || null;
140 return true;
141 } else {
142 return false;
143 }
144 },
145 async getRefreshToken() {
146 const request = axios.post(`${API_AUTH}token?grant_type=refresh_token&refresh_token=${this.refresh_token}`, {}, {
147 headers: {
148 'Content-Type': 'application/x-www-form-urlencoded',
149 Authorization: `Basic ${this.Authorization}`,
150 },
151 });
152
153 if (this.isCorrectRequest(request.status)) {
154 this.access_token = response.data.access_token;
155 this.refresh_token = response.data.refresh_token;
156 return true;
157 } else {
158 return false;
159 }
160 },
161 async getUsers() {
162 const request = await axios.get(`${API_USERS}accounts/${this.account_key}/users/`, {
163 headers: {
164 'Content-Type': 'application/x-www-form-urlencoded',
165 Accept: 'application/json',
166 Authorization: `Bearer ${this.access_token}`,
167 },
168 });
169 if (this.isCorrectRequest(request.status)) {
170 this.users = request.data.results;
171 return true;
172 } else {
173 return false;
174 }
175 },
176 async getTrainings() {
177 const request = await axios.get(`${API_GTT}organizers/${this.organizer_key}/trainings`, {
178 headers: {
179 'Content-Type': 'application/x-www-form-urlencoded',
180 Accept: 'application/json',
181 Authorization: `Bearer ${this.access_token}`,
182 },
183 });
184 if (this.isCorrectRequest(request.status)) {
185 this.trainings = request.data;
186 return true;
187 } else {
188 alert("Error : can't get trainings on GTT Api");
189 return false;
190 }
191 },
192
193 async getTrainingsRegistrants(trainings = null) {
194 if (!trainings) trainings = this.trainings;
195 trainings.forEach(async (training) => {
196 const request = await axios.get(`${API_GTT}organizers/${this.organizer_key}/trainings/${training.trainingKey}/registrants`, {
197 headers: {
198 'Content-Type': 'application/x-www-form-urlencoded',
199 Accept: 'application/json',
200 Authorization: `Bearer ${this.access_token}`,
201 },
202 })
203 training.participants = request.data;
204 })
205 }
206 }
207})
208</script>
209
210<style>
211 #app div {
212 font-size: 12px;
213 }
214 #app div span {
215 font-weight: bold;
216 font-size: 14px;
217 }
218 #app table {
219 border: 1px solid #666;
220 }
221 #app table tr {
222 border: 1px solid #666;
223 }
224 #app table td:not(:last-child) {
225 border-right: 1px solid #666;
226 }
227</style>