|
1 | 1 | // initialize the express application |
2 | | -var express = require("express"), |
3 | | - app = express(); |
| 2 | +const express = require("express"); |
| 3 | +const app = express(); |
4 | 4 |
|
5 | 5 | // initialize the Fitbit API client |
6 | | -var FitbitApiClient = require("fitbit-node"), |
7 | | - client = new FitbitApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET"); |
| 6 | +const FitbitApiClient = require("fitbit-node"); |
| 7 | +const client = new FitbitApiClient({ |
| 8 | + clientId: "YOUR_CLIENT_ID", |
| 9 | + clientSecret: "YOUR_CLIENT_SECRET", |
| 10 | + apiVersion: '1.2' // 1.2 is the default |
| 11 | +}); |
8 | 12 |
|
9 | 13 | // redirect the user to the Fitbit authorization page |
10 | | -app.get("/authorize", function (req, res) { |
11 | | - // request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes |
12 | | - res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'YOUR_CALLBACK_URL')); |
| 14 | +app.get("/authorize", (req, res) => { |
| 15 | + // request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes |
| 16 | + res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'YOUR_CALLBACK_URL')); |
13 | 17 | }); |
14 | 18 |
|
15 | 19 | // handle the callback from the Fitbit authorization flow |
16 | | -app.get("/callback", function (req, res) { |
17 | | - // exchange the authorization code we just received for an access token |
18 | | - client.getAccessToken(req.query.code, 'YOUR_CALLBACK_URL').then(function (result) { |
19 | | - // use the access token to fetch the user's profile information |
20 | | - client.get("/profile.json", result.access_token).then(function (results) { |
21 | | - res.send(results[0]); |
22 | | - }); |
23 | | - }).catch(function (error) { |
24 | | - res.send(error); |
25 | | - }); |
| 20 | +app.get("/callback", (req, res) => { |
| 21 | + // exchange the authorization code we just received for an access token |
| 22 | + client.getAccessToken(req.query.code, 'YOUR_CALLBACK_URL').then(result => { |
| 23 | + // use the access token to fetch the user's profile information |
| 24 | + client.get("/profile.json", result.access_token).then(results => { |
| 25 | + res.send(results[0]); |
| 26 | + }); |
| 27 | + }).catch(res.send); |
26 | 28 | }); |
27 | 29 |
|
28 | 30 | // launch the server |
|
0 commit comments