├── LICENSE
├── README.md
├── app
└── index.js
├── companion
└── index.js
├── package.json
├── resources
├── icon.png
├── images
│ ├── awake.jpg
│ ├── awake~300x300.jpg
│ ├── sleepy.jpg
│ ├── sleepy~300x300.jpg
│ ├── waiting.jpg
│ └── waiting~300x300.jpg
├── index.gui
├── styles.css
└── widgets.gui
└── settings
└── index.jsx
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Fitbit, Inc
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # sdk-oauth
2 |
3 | A sample application which uses the Settings API [OAuth
4 | component](https://dev.fitbit.com/reference/settings-api/#oauth-button), and the
5 | [Fitbit Web API](https://dev.fitbit.com/reference/web-api/quickstart/) to query
6 | sleep data.
7 |
8 | ## Usage
9 |
10 | 1. You must first register a Web Application on
11 | [dev.fitbit.com](https://dev.fitbit.com/apps/new) to get an OAuth ID and
12 | secret. Configure the application as:
13 |
14 | - OAuth 2.0 Application Type: **Server**
15 | - Callback URL:
16 | **https://app-settings.fitbitdevelopercontent.com/simple-redirect.html**
17 |
18 | 2. Enter your **OAuth 2.0 Client ID** and **Client Secret** into
19 | `settings/index.jsx`
20 |
21 | 3. After installing the project from Fitbit Studio, you need to login to the
22 | Fitbit Web API using the settings page within the Fitbit mobile application.
23 |
24 | ***Fitbit mobile app > Ionic > Developer menu > your app > Settings***
25 |
26 | Read more in the [Reference
27 | documentation](https://dev.fitbit.com/reference/#overview).
28 |
--------------------------------------------------------------------------------
/app/index.js:
--------------------------------------------------------------------------------
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 | };
17 |
--------------------------------------------------------------------------------
/companion/index.js:
--------------------------------------------------------------------------------
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 date = new Date();
7 | let todayDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`; //YYYY-MM-DD
8 |
9 | // Sleep API docs - https://dev.fitbit.com/reference/web-api/sleep/
10 | fetch(`https://api.fitbit.com/1.2/user/-/sleep/date/${todayDate}.json`, {
11 | method: "GET",
12 | headers: {
13 | "Authorization": `Bearer ${accessToken}`
14 | }
15 | })
16 | .then(function(res) {
17 | return res.json();
18 | })
19 | .then(function(data) {
20 | let myData = {
21 | totalMinutesAsleep: data.summary.totalMinutesAsleep
22 | }
23 | if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
24 | messaging.peerSocket.send(myData);
25 | }
26 | })
27 | .catch(err => console.log('[FETCH]: ' + err));
28 | }
29 |
30 | // A user changes Settings
31 | settingsStorage.onchange = evt => {
32 | if (evt.key === "oauth") {
33 | // Settings page sent us an oAuth token
34 | let data = JSON.parse(evt.newValue);
35 | fetchSleepData(data.access_token) ;
36 | }
37 | };
38 |
39 | // Restore previously saved settings and send to the device
40 | function restoreSettings() {
41 | for (let index = 0; index < settingsStorage.length; index++) {
42 | let key = settingsStorage.key(index);
43 | if (key && key === "oauth") {
44 | // We already have an oauth token
45 | let data = JSON.parse(settingsStorage.getItem(key))
46 | fetchSleepData(data.access_token);
47 | }
48 | }
49 | }
50 |
51 | // Message socket opens
52 | messaging.peerSocket.onopen = () => {
53 | restoreSettings();
54 | };
55 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "fitbit": {
3 | "appType": "app",
4 | "appDisplayName": "OAuth Example",
5 | "iconFile": "resources/icon.png",
6 | "wipeColor": "#ffeb3b",
7 | "requestedPermissions": [
8 | "access_internet"
9 | ],
10 | "buildTargets": [
11 | "higgs",
12 | "meson"
13 | ],
14 | "i18n": {
15 | "en": {
16 | "name": "OAuth Example"
17 | }
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/resources/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/icon.png
--------------------------------------------------------------------------------
/resources/images/awake.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/images/awake.jpg
--------------------------------------------------------------------------------
/resources/images/awake~300x300.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/images/awake~300x300.jpg
--------------------------------------------------------------------------------
/resources/images/sleepy.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/images/sleepy.jpg
--------------------------------------------------------------------------------
/resources/images/sleepy~300x300.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/images/sleepy~300x300.jpg
--------------------------------------------------------------------------------
/resources/images/waiting.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/images/waiting.jpg
--------------------------------------------------------------------------------
/resources/images/waiting~300x300.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/images/waiting~300x300.jpg
--------------------------------------------------------------------------------
/resources/index.gui:
--------------------------------------------------------------------------------
1 |
4 |
--------------------------------------------------------------------------------
/resources/styles.css:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Fitbit/sdk-oauth/591da4bc36ac68a5c71415cd13c13d493653ae3e/resources/styles.css
--------------------------------------------------------------------------------
/resources/widgets.gui:
--------------------------------------------------------------------------------
1 |
7 |
--------------------------------------------------------------------------------
/settings/index.jsx:
--------------------------------------------------------------------------------
1 | function mySettings(props) {
2 | return (
3 |
4 | Fitbit Account}>
6 |
17 |
18 |
19 | );
20 | }
21 |
22 | registerSettingsPage(mySettings);
23 |
--------------------------------------------------------------------------------