Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fitbit-client-oauth2

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fitbit-client-oauth2 - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

example/auth_server.js

9

package.json
{
"name": "fitbit-client-oauth2",
"version": "1.0.1",
"version": "2.0.0",
"description": "A fitbit client using OAuth 2.0 authentication.",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha --reporter spec"
},

@@ -29,3 +29,8 @@ "repository": {

"simple-oauth2": "^0.2.1"
},
"devDependencies": {
"chai": "^3.2.0",
"mocha": "^2.2.5",
"nock": "^2.10.0"
}
}

@@ -5,41 +5,96 @@ # Fitbit client

**WARNING**: Every release should be usable and stable, but it is a Work in Progress until all Fitbit's API is covered.
## Usage
### Token object
Every method of the API needs a valid token object with these properties:
* `access_token`: a valid user's access token.
* `refresh_token`: the user's refresh token. Optional for every call except `refreshAccessToken()`
* `expires_in`: expiration time in seconds. Optional.
### Using an existing user token
```
var FitbitClient = require('fitbit-client-oauth2');
var FitbitClient = require('fitbit-client-oauth2');
var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET> );
// retrieve previously saved user's token from db or somewhere
var tokens = existingUser.fitbitTokens;
var options = { /* TIME_SERIES_OPTIONS */ };
client.getTimeSeries(tokens, options)
.then(function(res) {
console.log('results: ', res);
}).catch(function(err) {
console.log('error getting user data', err);
});
var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET> );
```
// retrieve previously saved user's token from db
client.setTokens(access_token, refresh_token, expires_in);
### Refreshing an expired user token
client.getTimeSeries().then(function(res) {
```
client.refreshAccessToken(tokens)
.then(function(new_token) {
// save new_token data to db
// then do more stuff here.
}).catch(function(err) {
console.log('error refreshing user token', err);
});
console.log('results: ', res);
```
}).catch(function(err) {
console.log('error getting user data', err);
});
### Get an access token
```
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.
### Refreshing an expired user token
```
client.refreshAccessToken().then(function(token) {
**NOTE**: You can also use [passport-fitbit-oauth2](https://github.com/thegameofcode/passport-fitbit-oauth2) instead of doing this manually.
```
var client = new FitbitClient(<YOUR_FITBIT_API_KEY>, <YOUR_FITBIT_API_SECRET>);
var redirect_uri = 'http://redirect_uri_used_in_fitbit_app_website';
var scope = [ 'activity', 'nutrition', 'profile', 'settings', 'sleep', 'social', 'weight' ];
// save new token data to db
// do more stuff here.
server.get('/auth/fitbit', function(req, res, next) {
}).catch(function(err) {
console.log('error refreshing user token', err);
var authorization_uri = client.getAuthorizationUrl(redirect_uri, scope);
res.redirect(authorization_uri);
});
// If /auth/fitbit/callbac is your redirec_uri
server.get('/auth/fitbit/callback', function(req, res, next) {
var code = req.query.code;
client.getToken(code, redirect_uri)
.then(function(token) {
// ... save your token on db or session...
// then redirect
res.redirect(302, '/user');
})
.catch(function(err) {
// something went wrong.
res.send(500, err);
});
});
```
## TODO
* Implement full OAuth authorization code flow. (now it relays on [passport-fitbit-oauth2](https://github.com/thegameofcode/passport-fitbit-oauth2)).
* Implement full OAuth authorization code flow. (use it on Connect servers with [passport-fitbit-oauth2](https://github.com/thegameofcode/passport-fitbit-oauth2)).
* Cover more of the Fitbit API endpoints

@@ -46,0 +101,0 @@ * Add token expiration event to the client (EventEmitter).

var oauth2 = require('simple-oauth2');
var Promise = require('promise');
var helper = require('./helpers');
var config = require('./config');
var addAuth = require('./auth');
var addAPI = require('./api');
var proto;
var FitbitClient = function(clientId, consumerSecret, options) {
var FitbitClient = function(clientId, consumerSecret) {
options = options || {};
if (!clientId) {
throw new Error('Missing clientId parameter');
}
if (!consumerSecret) {
throw new Error('Missing consumerSecret parameter');
}
// Set the client credentials and the OAuth2 server

@@ -21,62 +29,10 @@ this.oauth2 = oauth2({

};
this.redirect_uri = options.redirect_uri;
this.scope = options.scope || config.FITBIT_DEFAULT_SCOPE;
proto = FitbitClient.prototype;
proto.setTokens = function(accessToken, refreshToken, expiresIn) {
this.token = this.oauth2.accessToken.create({
access_token: accessToken,
refresh_token: refreshToken,
expires_in: expiresIn
});
};
proto.refreshAccessToken = function(options) {
var _this = this;
options = options || {};
addAuth(FitbitClient.prototype);
addAPI(FitbitClient.prototype);
if( !this.token.expired() && !options.forceRefresh) {
return Promise.resolve(this.token);
}
return new Promise(function(resolve, reject) {
_this.token.refresh(function(err, token) {
if (err) {
return reject(err);
}
_this.token = token;
return resolve(token);
});
});
};
proto.getDailyActivitySummary = function(options) {
options = helper.buildDailyActivitySummaryOptions(options);
//TODO: improve this way of getting the token
options.access_token = this.token.token.access_token;
return helper.createRequestPromise(options);
};
proto.getTimeSeries = function(options) {
options = helper.buildTimeSeriesOptions(options);
//TODO: improve this way of getting the token
options.access_token = this.token.token.access_token;
return helper.createRequestPromise(options);
};
proto.getIntradayTimeSeries = function(options) {
options = helper.buildIntradayTimeSeriesOptions(options);
options.access_token = this.token.token.access_token;
return helper.createRequestPromise(options);
};
module.exports = FitbitClient;
const FITBIT_BASE_API_URL = 'https://api.fitbit.com';
const FITBIT_AUTH_PATH = '/oauth2/authorize';
const FITBIT_TOKEN_PATH = '/oauth2/token';
const FITBIT_DEFAULT_SCOPE = [ 'activity', 'nutrition', 'profile', 'settings', 'sleep', 'social', 'weight' ];

@@ -8,3 +9,4 @@ module.exports = {

FITBIT_AUTH_PATH: FITBIT_AUTH_PATH,
FITBIT_TOKEN_PATH: FITBIT_TOKEN_PATH
FITBIT_TOKEN_PATH: FITBIT_TOKEN_PATH,
FITBIT_DEFAULT_SCOPE: FITBIT_DEFAULT_SCOPE
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc