-
Notifications
You must be signed in to change notification settings - Fork 37
/
example.js
35 lines (31 loc) · 1.23 KB
/
example.js
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
// initialize the express application
const express = require("express");
const app = express();
// initialize the Fitbit API client
const FitbitApiClient = require("fitbit-node");
const client = new FitbitApiClient({
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
apiVersion: '1.2' // 1.2 is the default
});
// redirect the user to the Fitbit authorization page
app.get("/authorize", (req, res) => {
// request access to the user's activity, heartrate, location, nutrion, profile, settings, sleep, social, and weight scopes
res.redirect(client.getAuthorizeUrl('activity heartrate location nutrition profile settings sleep social weight', 'YOUR_CALLBACK_URL'));
});
// handle the callback from the Fitbit authorization flow
app.get("/callback", (req, res) => {
// exchange the authorization code we just received for an access token
client.getAccessToken(req.query.code, 'YOUR_CALLBACK_URL').then(result => {
// use the access token to fetch the user's profile information
client.get("/profile.json", result.access_token).then(results => {
res.send(results[0]);
}).catch(err => {
res.status(err.status).send(err);
});
}).catch(err => {
res.status(err.status).send(err);
});
});
// launch the server
app.listen(3000);