forked from lukasolson/fitbit-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
29 lines (25 loc) · 1.17 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
// initialize the express application
var express = require("express"),
app = express();
// initialize the Fitbit API client
var FitbitApiClient = require("fitbit-node"),
client = new FitbitApiClient("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET");
// redirect the user to the Fitbit authorization page
app.get("/authorize", function (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", function (req, res) {
// exchange the authorization code we just received for an access token
client.getAccessToken(req.query.code, 'YOUR_CALLBACK_URL').then(function (result) {
// use the access token to fetch the user's profile information
client.get("/profile.json", result.access_token).then(function (results) {
res.send(results[0]);
});
}).catch(function (error) {
res.send(error);
});
});
// launch the server
app.listen(3000);