-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmljs_implementation.html
92 lines (79 loc) · 3.29 KB
/
htmljs_implementation.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wearable Data Proof of Concept</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 text-white min-h-screen flex items-center justify-center">
<div class="mx-auto">
<h1 class="text-4xl font-bold mb-4">Wearable Data Proof of Concept</h1>
<button id="signInButton" class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded">
Sign in with Google
</button>
<button id="getHealthDataButton" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
style="display: none;">
Get User Health Data
</button>
<div id="healthDataContainer" class="mt-4"></div>
</div>
<script>
const CLIENT_ID = '546668630563-jo63fhpu6rmgd19jjovmgkvb37pnu9pd.apps.googleusercontent.com';
const SCOPE = 'https://www.googleapis.com/auth/fitness.activity.read';
let accessToken = '';
const signInButton = document.getElementById('signInButton');
const getHealthDataButton = document.getElementById('getHealthDataButton');
const healthDataContainer = document.getElementById('healthDataContainer');
signInButton.addEventListener('click', handleGoogleSignIn);
getHealthDataButton.addEventListener('click', handleHealthDataButtonClick);
function handleGoogleSignIn() {
const authUrl = `https://accounts.google.com/o/oauth2/auth?client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(window.location.origin)}&response_type=token&scope=${encodeURIComponent(SCOPE)}`;
window.location.href = authUrl;
}
async function handleHealthDataButtonClick() {
try {
const response = await fetch('https://www.googleapis.com/fitness/v1/users/me/dataSources', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
const data = await response.json();
displayHealthData(data);
} catch (error) {
console.error('Error fetching user health data:', error);
}
}
function displayHealthData(data) {
if (!data || !data.dataSource) {
healthDataContainer.innerHTML = '';
return;
}
const healthDataHTML = `
<h2 class="text-2xl font-bold mb-2">User Health Data</h2>
<div class="grid grid-cols-2 gap-4">
${data.dataSource.map((source) => `
<div class="bg-gray-800 p-4 rounded shadow">
<h3 class="text-xl font-semibold mb-2">${source.dataStreamName}</h3>
<p>Type: ${source.type}</p>
<p>Data Type: ${source.dataType.name}</p>
</div>
`).join('')}
</div>
`;
healthDataContainer.innerHTML = healthDataHTML;
}
function handleRedirect() {
const params = new URLSearchParams(window.location.hash.slice(1));
const token = params.get('access_token');
if (token) {
accessToken = token;
window.history.replaceState(null, '', window.location.pathname);
signInButton.style.display = 'none';
getHealthDataButton.style.display = 'inline-block';
}
}
window.addEventListener('load', handleRedirect);
</script>
</body>
</html>