-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
180 lines (173 loc) · 7.12 KB
/
index.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Auth Code Flow Tutorial - PHP</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>PHP Authorization Code Flow Tutorial - SKY API</h1>
<p class="lead">The following is a demonstration of the authorization code flow within SKY API, implemented using <a href="https://secure.php.net/" target="_blank">PHP</a>.</p>
<div class="content">
<a href="/auth/login.php" class="btn btn-primary" id="login-button">Log in</a>
<div id="constituent-data-section" class="hidden">
<div class="well">
<h3 id="constituent-name"></h3>
<p>
See <a href="https://developer.blackbaud.com/skyapi/apis/constituent/entities#Constituent" target="_blank">Constituent</a>
within the Blackbaud SKY API contact reference for a full listing of properties.
</p>
</div>
<div id="constituent-data"></div>
<button id="logout-button" class="btn btn-link">Log out</button>
<button id="refresh-token-button" class="btn btn-primary">Refresh Access Token</button>
<button id="session-button" class="btn btn-default">Session Token Info</button>
<button id="refresh-button" class="btn btn-default">Refresh</button>
<div id="token-response"></div>
<div id="session-response"></div>
</div>
</div>
</div>
</body>
</html>
<script>
(function () {
'use strict';
const loginButton = document.getElementById('login-button');
const constituentDataSection = document.getElementById('constituent-data-section');
const constituentName = document.getElementById('constituent-name');
const constituentData = document.getElementById('constituent-data');
const logoutButton = document.getElementById('logout-button');
const refreshTokenButton = document.getElementById('refresh-token-button');
const sessionButton = document.getElementById('session-button');
const tokenResponse = document.getElementById('token-response');
const sessionResponse = document.getElementById('session-response');
const refreshButton = document.getElementById('refresh-button');
// Check user access token.
fetch('/auth/authenticated.php')
.then(response => response.json())
.then(data => {
if (data.authenticated === true) {
// Access token is valid. Fetch constituent record.
fetch('/api/constituents.php?id=280')
.then(response => response.json())
.then(data => {
constituentName.innerHTML = `Constituent: ${data.constituent.name}`;
constituentData.innerHTML = `
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>${data.constituent.id}</td>
</tr>
<tr>
<td>type</td>
<td>${data.constituent.type}</td>
</tr>
<tr>
<td>lookup_id</td>
<td>${data.constituent.lookup_id}</td>
</tr>
<tr>
<td>first</td>
<td>${data.constituent.first}</td>
</tr>
<tr>
<td>last</td>
<td>${data.constituent.last}</td>
</tr>
</tbody>
</table>
`;
loginButton.classList.add('hidden');
constituentDataSection.classList.remove('hidden');
})
.catch(error => {
console.error(error);
loginButton.classList.remove('hidden');
constituentDataSection.classList.add('hidden');
});
}
});
logoutButton.addEventListener('click', () => {
fetch('/auth/logout.php', { method: 'POST' })
.then(() => window.location.reload());
});
refreshTokenButton.addEventListener('click', () => {
fetch('/auth/refresh-token.php')
.then(response => response.json())
.then(data => {
tokenResponse.innerHTML = `
<h4>Token Response:</h4>
<pre>${JSON.stringify(data, null, 2)}</pre>
`;
});
});
sessionButton.addEventListener('click', () => {
fetch('/auth/session-token.php')
.then(response => response.json())
.then(data => {
sessionResponse.innerHTML = `
<h4>Session Response:</h4>
<pre>${JSON.stringify(data, null, 2)}</pre>
`;
});
});
refreshButton.addEventListener('click', () => {
fetch('/api/constituents.php?id=280')
.then(response => response.json())
.then(data => {
constituentName.innerHTML = `Constituent: ${data.constituent.name}`;
constituentData.innerHTML = `
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>id</td>
<td>${data.constituent.id}</td>
</tr>
<tr>
<td>type</td>
<td>${data.constituent.type}</td>
</tr>
<tr>
<td>lookup_id</td>
<td>${data.constituent.lookup_id}</td>
</tr>
<tr>
<td>first</td>
<td>${data.constituent.first}</td>
</tr>
<tr>
<td>last</td>
<td>${data.constituent.last}</td>
</tr>
</tbody>
</table>
`;
})
.catch(error => {
console.error(error);
});
});
})();
</script>