Skip to content

Commit b088013

Browse files
author
Jon Barlow
committed
Added example
1 parent 20e5fcc commit b088013

File tree

12 files changed

+139
-1
lines changed

12 files changed

+139
-1
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
11
# sdk-oauth
2-
A sample application which uses the Settings API oAuth component, and the Fitbit Web API to query sleep data.
2+
3+
A sample application which uses the Settings API OAuth component, and the Fitbit
4+
Web API to query sleep data.
5+
6+
## Usage
7+
8+
1. You must first register a Web Application on
9+
[dev.fitbit.com](https://dev.fitbit.com/apps/new).
10+
11+
- OAuth 2.0 Application Type: **Server**
12+
- Callback URL:
13+
**https://app-settings.fitbitdevelopercontent.com/simple-redirect.html**
14+
15+
2. Enter your **OAuth 2.0 Client ID** and **Client Secret** into
16+
`settings/index.jsx`
17+
18+
Read more in the [Reference
19+
documentation](https://dev.fitbit.com/reference/#overview).

app/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import document from "document";
2+
import * as messaging from "messaging";
3+
4+
let myImage = document.getElementById("myImage");
5+
6+
// Message is received from companion
7+
messaging.peerSocket.onmessage = evt => {
8+
// Am I Tired?
9+
if (evt.data.totalMinutesAsleep >= 300) {
10+
// Had at least 5 hours sleep
11+
myImage.href = "images/awake.jpg";
12+
} else {
13+
// Had less than 5 hours sleep
14+
myImage.href = "images/sleepy.jpg";
15+
}
16+
};

companion/index.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as messaging from "messaging";
2+
import { settingsStorage } from "settings";
3+
4+
// Fetch Sleep Data from Fitbit Web API
5+
function fetchSleepData(accessToken) {
6+
let todayDate = new Date().toISOString().slice(0,10); //YYYY-MM-DD
7+
8+
fetch(`https://api.fitbit.com/1.2/user/-/sleep/date/${todayDate}.json`, {
9+
method: "GET",
10+
headers: {
11+
"Authorization": `Bearer ${accessToken}`
12+
}
13+
})
14+
.then(function(res) {
15+
return res.json();
16+
})
17+
.then(function(data) {
18+
let myData = {
19+
totalMinutesAsleep: data.summary.totalMinutesAsleep
20+
}
21+
sendVal(myData);
22+
})
23+
.catch(err => console.log('[FETCH]: ' + err));
24+
}
25+
26+
// Send data to the device
27+
function sendVal(data) {
28+
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
29+
messaging.peerSocket.send(data);
30+
}
31+
}
32+
33+
// A user changes Settings
34+
settingsStorage.onchange = evt => {
35+
if (evt.key === "oauth") {
36+
// Settings page sent us an oAuth token
37+
let data = JSON.parse(evt.newValue);
38+
fetchSleepData(data.access_token) ;
39+
}
40+
};
41+
42+
// Restore previously saved settings and send to the device
43+
function restoreSettings() {
44+
for (let index = 0; index < settingsStorage.length; index++) {
45+
let key = settingsStorage.key(index);
46+
if (key && key === "oauth") {
47+
// We already have an oauth token
48+
let data = JSON.parse(settingsStorage.getItem(key))
49+
fetchSleepData(data.access_token);
50+
}
51+
}
52+
}
53+
54+
// Message socket opens
55+
messaging.peerSocket.onopen = () => {
56+
restoreSettings();
57+
};

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"fitbit": {
3+
"appUUID": "f09e8a1b-34dd-43bb-95c9-21c351ed4ed4",
4+
"appType": "app",
5+
"appDisplayName": "Am I Tired?",
6+
"iconFile": "resources/icon.png",
7+
"wipeColor": "#ffeb3b",
8+
"requestedPermissions": [
9+
"access_internet"
10+
],
11+
"i18n": {
12+
"en": {
13+
"name": "Am I Tired?"
14+
}
15+
}
16+
}
17+
}

resources/icon.png

8.34 KB
Loading

resources/images/awake.jpg

7.5 KB
Loading

resources/images/sleepy.jpg

8.58 KB
Loading

resources/images/waiting.jpg

8.21 KB
Loading

resources/index.gui

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<svg>
2+
<image id="myImage" href="images/waiting.jpg" />
3+
</svg>

resources/styles.css

Whitespace-only changes.

resources/widgets.gui

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<svg>
2+
<defs>
3+
<link rel="stylesheet" href="styles.css" />
4+
<link rel="import" href="/mnt/sysassets/widgets_common.gui" />
5+
</defs>
6+
</svg>

settings/index.jsx

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function mySettings(props) {
2+
return (
3+
<Page>
4+
<Section
5+
title={<Text bold align="center">Fitbit Account</Text>}>
6+
<Oauth
7+
settingsKey="oauth"
8+
title="Login"
9+
label="Fitbit"
10+
status="Login"
11+
authorizeUrl="https://www.fitbit.com/oauth2/authorize"
12+
requestTokenUrl="https://api.fitbit.com/oauth2/token"
13+
clientId="XXXXXXXXXX"
14+
clientSecret="XXXXXXXXXX"
15+
scope="profile sleep"
16+
/>
17+
</Section>
18+
</Page>
19+
);
20+
}
21+
22+
registerSettingsPage(mySettings);

0 commit comments

Comments
 (0)