-
Notifications
You must be signed in to change notification settings - Fork 2
/
client_credentials.html
72 lines (60 loc) · 1.55 KB
/
client_credentials.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Client Credentials</title>
</head>
<body>
<div id="wrapper">
<form method="POST">
<input type="submit" id="token" value="Obtener Token" />
</form>
</div>
<script>
(function () {
'use strict';
const clientId = 2;
const clientSecret = 'Your-Client-Secret';
const grantType = 'client_credentials';
let token = document.getElementById('token');
if ( localStorage.getItem('client_credentials') ) {
document.getElementById('wrapper').innerHTML = '¡Autenticado!';
fetch('http://localhost:8080/api/clients/posts', {
method: 'POST',
body: JSON.stringify({
title: 'Esto es una prueba!!',
body: 'Descripción usando Client Credentials'
}),
headers: { 'Authorization': 'Bearer ' + localStorage.getItem('client_credentials'), 'Content-Type': 'application/json' }
})
.then(response => {
return response.json()
})
.then(data => {
console.log(data);
})
return;
}
token.addEventListener('click', e => {
e.preventDefault();
fetch('http://localhost:8080/oauth/token', {
method: 'POST',
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
grant_type: grantType
}),
headers: { 'Content-Type': 'application/json' },
})
.then(response => {
return response.json()
})
.then(data => {
localStorage.setItem('client_credentials', data.access_token);
location.reload();
})
});
})();
</script>
</body>
</html>