Skip to content

Commit 716bc92

Browse files
committed
updated README.
1 parent 4543a17 commit 716bc92

File tree

1 file changed

+75
-20
lines changed

1 file changed

+75
-20
lines changed

README.md

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,100 @@
22

33
An OAuth 2.0 client to consume [Fitbit's API](http://www.fitbit.com/).
44

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.
86

9-
```
10-
var FitbitClient = require('fitbit-client-oauth2');
7+
## Usage
118

12-
var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET> );
9+
### Token object
1310

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:
1612

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.
1816

19-
console.log('results: ', res);
17+
### Using an existing user token
2018

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+
});
2435
2536
```
2637

2738
### Refreshing an expired user token
2839

2940
```
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' ];
3162
32-
// save new token data to db
33-
// do more stuff here.
63+
server.get('/auth/fitbit', function(req, res, next) {
3464
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);
3768
});
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+
3993
```
4094

95+
4196
## TODO
4297

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)).
4499
* Cover more of the Fitbit API endpoints
45100
* Add token expiration event to the client (EventEmitter).
46101
* Implement automatic retries on token expiration errors

0 commit comments

Comments
 (0)