|
2 | 2 |
|
3 | 3 | An OAuth 2.0 client to consume [Fitbit's API](http://www.fitbit.com/).
|
4 | 4 |
|
5 |
| -## Usage |
6 |
| - |
7 |
| -### Using an existing user token |
| 5 | +**WARNING**: Every release should be usable and stable, but it is a Work in Progress until all Fitbit's API is covered. |
8 | 6 |
|
9 |
| -``` |
10 |
| -var FitbitClient = require('fitbit-client-oauth2'); |
| 7 | +## Usage |
11 | 8 |
|
12 |
| -var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET> ); |
| 9 | +### Token object |
13 | 10 |
|
14 |
| -// retrieve previously saved user's token from db |
15 |
| -client.setTokens(access_token, refresh_token, expires_in); |
| 11 | +Every method of the API needs a valid token object with these properties: |
16 | 12 |
|
17 |
| -client.getTimeSeries().then(function(res) { |
| 13 | + * `access_token`: a valid user's access token. |
| 14 | + * `refresh_token`: the user's refresh token. Optional for every call except `refreshAccessToken()` |
| 15 | + * `expires_in`: expiration time in seconds. Optional. |
18 | 16 |
|
19 |
| - console.log('results: ', res); |
| 17 | +### Using an existing user token |
20 | 18 |
|
21 |
| -}).catch(function(err) { |
22 |
| - console.log('error getting user data', err); |
23 |
| -}); |
| 19 | +``` |
| 20 | + var FitbitClient = require('fitbit-client-oauth2'); |
| 21 | + |
| 22 | + var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET> ); |
| 23 | + |
| 24 | + // retrieve previously saved user's token from db or somewhere |
| 25 | + var tokens = existingUser.fitbitTokens; |
| 26 | + |
| 27 | + var options = { /* TIME_SERIES_OPTIONS */ }; |
| 28 | + |
| 29 | + client.getTimeSeries(tokens, options) |
| 30 | + .then(function(res) { |
| 31 | + console.log('results: ', res); |
| 32 | + }).catch(function(err) { |
| 33 | + console.log('error getting user data', err); |
| 34 | + }); |
24 | 35 |
|
25 | 36 | ```
|
26 | 37 |
|
27 | 38 | ### Refreshing an expired user token
|
28 | 39 |
|
29 | 40 | ```
|
30 |
| - client.refreshAccessToken().then(function(token) { |
| 41 | + client.refreshAccessToken(tokens) |
| 42 | + .then(function(new_token) { |
| 43 | + // save new_token data to db |
| 44 | + // then do more stuff here. |
| 45 | + |
| 46 | + }).catch(function(err) { |
| 47 | + console.log('error refreshing user token', err); |
| 48 | + }); |
| 49 | +
|
| 50 | +``` |
| 51 | + |
| 52 | +### Get an access token |
| 53 | + |
| 54 | + If you need to start an OAuth flow to get user's permission and access_token, you need to redirect to Fitbit's OAuth endpoint. |
| 55 | + |
| 56 | + **NOTE**: You can also use [passport-fitbit-oauth2](https://github.com/thegameofcode/passport-fitbit-oauth2) instead of doing this manually. |
| 57 | + |
| 58 | +``` |
| 59 | + var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET>); |
| 60 | + var redirect_uri = 'http://redirect_uri_used_in_fitbit_app_website'; |
| 61 | + var scope = [ 'activity', 'nutrition', 'profile', 'settings', 'sleep', 'social', 'weight' ]; |
31 | 62 |
|
32 |
| - // save new token data to db |
33 |
| - // do more stuff here. |
| 63 | + server.get('/auth/fitbit', function(req, res, next) { |
34 | 64 |
|
35 |
| - }).catch(function(err) { |
36 |
| - console.log('error refreshing user token', err); |
| 65 | + var authorization_uri = client.getAuthorizationUrl(redirect_uri, scope); |
| 66 | + |
| 67 | + res.redirect(authorization_uri); |
37 | 68 | });
|
38 |
| -
|
| 69 | + |
| 70 | + // If /auth/fitbit/callbac is your redirec_uri |
| 71 | + |
| 72 | + server.get('/auth/fitbit/callback', function(req, res, next) { |
| 73 | + |
| 74 | + var code = req.query.code; |
| 75 | + |
| 76 | + client.getToken(code, redirect_uri) |
| 77 | + .then(function(token) { |
| 78 | +
|
| 79 | + // ... save your token on db or session... |
| 80 | + |
| 81 | + // then redirect |
| 82 | + res.redirect(302, '/user'); |
| 83 | +
|
| 84 | + }) |
| 85 | + .catch(function(err) { |
| 86 | + // something went wrong. |
| 87 | + res.send(500, err); |
| 88 | + |
| 89 | + }); |
| 90 | + |
| 91 | + }); |
| 92 | + |
39 | 93 | ```
|
40 | 94 |
|
| 95 | + |
41 | 96 | ## TODO
|
42 | 97 |
|
43 |
| - * Implement full OAuth authorization code flow. (now it relays on [passport-fitbit-oauth2](https://github.com/thegameofcode/passport-fitbit-oauth2)). |
| 98 | + * Implement full OAuth authorization code flow. (use it on Connect servers with [passport-fitbit-oauth2](https://github.com/thegameofcode/passport-fitbit-oauth2)). |
44 | 99 | * Cover more of the Fitbit API endpoints
|
45 | 100 | * Add token expiration event to the client (EventEmitter).
|
46 | 101 | * Implement automatic retries on token expiration errors
|
|
0 commit comments