Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

hello, fitbit

  • Loading branch information...
commit d3976c39a571c851eacdbcb19b39be478a919e4f 0 parents
@smurthas authored
1  .gitignore
@@ -0,0 +1 @@
+node_modules
23 README.md
@@ -0,0 +1,23 @@
+# fitbit-js
+
+Simple FitBit API client for express + connect.
+
+ npm install fitbit-js
+
+## Usage
+
+fitbit-js has two methods:
+
+* getAccessToken(_req_, _res_, _callback_): Uses oAuth module to get the access_token
+* apiCall(_http_method_, _path_, _params_, _callback_): Does a call to the FitBit API.
+
+Params must contain the token.
+
+## Test
+
+Enter your consumer key and secret in example/test.js
+
+ cd test
+ node test.js
+
+open http://localhost:8553
31 example/test.js
@@ -0,0 +1,31 @@
+var express = require('express'),
+ connect = require('connect'),
+ app = express.createServer(connect.bodyParser(),
+ connect.cookieParser(),
+ connect.session({secret: 'session'}));
+var fs = require('fs');
+var fitbitClient = require('../')('yourConsumerKey', 'yourConsumerSecret'),
+
+var token;
+app.get('/', function (req, res) {
+ fitbitClient.getAccessToken(req, res, function (error, newToken) {
+ if(newToken) {
+ token = newToken;
+ res.writeHead(200, {'Content-Type':'text/html'});
+ res.end('<html>Now <a href="/getStuff">get stuff</a></html>');
+ }
+ });
+});
+
+app.get('/getStuff', function (req, res) {
+ fitbitClient.apiCall('GET', '/user/-/activities/date/2011-05-25.json',
+ {token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
+ function(err, resp) {
+ res.writeHead(200, 'application/json');
+ res.end(JSON.stringify(resp));
+ });
+
+});
+
+app.listen(8553);
+console.log('listening at http://localhost:8553/');
91 lib/fitbit_client.js
@@ -0,0 +1,91 @@
+/*
+ * Fitbit-js
+ *
+ * Copyright (c) 2011 Simon Murtha-Smith <simon@murtha-smith.com>
+ */
+
+var url = require("url"),
+ http = require('http'),
+ OAuth = require('oauth').OAuth,
+ querystring = require("querystring");
+
+var baseURI = 'http://api.fitbit.com/1';
+
+module.exports = function (api_key, api_secret, callbackURI) {
+ var client = {version: '0.0.0'};
+
+ var oAuth = new OAuth('http://api.fitbit.com/oauth/request_token',
+ 'http://api.fitbit.com/oauth/access_token',
+ api_key, api_secret, '1.0', callbackURI,
+ 'HMAC-SHA1', null,
+ {'Accept': '*/*', 'Connection': 'close', 'User-Agent': 'fitbit-js ' + client.version});
+
+
+ function requestCallback(callback) {
+ return function (err, data, response) {
+ if (err) {
+ callback(err, data);
+ } else {
+ try {
+ callback(null, JSON.parse(data));
+ } catch (exc) {
+ callback(exc, data);
+ }
+ }
+ };
+ }
+
+ function get(path, params, token, callback) {
+ oAuth.get(baseURI + path + '?' + querystring.stringify(params),
+ token.oauth_token, token.oauth_token_secret, requestCallback(callback));
+ }
+
+ function post(path, params, token, callback) {
+ oAuth.post(baseURI + path, token.oauth_token, token.oauth_token_secret, params, null, requestCallback(callback));
+ }
+
+ // PUBLIC
+ client.apiCall = function (method, path, params, callback) {
+ var token = params.token;
+ delete params.token;
+ if (method === 'GET')
+ get(path, params, token, callback);
+ else if (method === 'POST')
+ post(path, params, token, callback);
+ }
+ }
+
+ client.getAccessToken = function (req, res, callback) {
+ var parsedUrl = url.parse(req.url, true),
+ callbackUrl = (req.socket.encrypted ? 'https' : 'http') + '://' + req.headers.host + parsedUrl.pathname,
+ has_token = parsedUrl.query && parsedUrl.query.oauth_token,
+ has_secret = req.session.auth && req.session.auth.fitbit_oauth_token_secret;
+
+ if(has_token && has_secret) { // Access token
+ oAuth.getOAuthAccessToken(parsedUrl.query.oauth_token,
+ req.session.auth.fitbit_oauth_token_secret,
+ parsedUrl.query.oauth_verifier,
+ function (error, oauth_token, oauth_token_secret, additionalParameters) {
+ if (error)
+ callback(error, null);
+ else
+ callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
+ });
+ } else { // Request token
+ oAuth.getOAuthRequestToken({oauth_callback: callbackUrl},
+ function (error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters) {
+ if(!error) {
+ req.session.fitbit_redirect_url = req.url;
+ req.session.auth = req.session.auth || {};
+ req.session.auth.fitbit_oauth_token_secret = oauth_token_secret;
+ req.session.auth.fitbit_oauth_token = oauth_token;
+ res.redirect("http://www.fitbit.com/oauth/authorize?oauth_token=" + oauth_token);
+ } else {
+ callback(error, null);
+ }
+ });
+ }
+ };
+
+ return client;
+};
12 package.json
@@ -0,0 +1,12 @@
+{
+ "name": "fitbit-js",
+ "description": "Simple FitBit API client",
+ "version": "0.0.0",
+ "author": "Simon Murtha-Smith <simon@murtha-smith.com>",
+ "keywords": ["fitbit"],
+ "main" : "lib/fitbit_client.js",
+ "directories" : { "lib" : "./lib" },
+ "dependencies": { "oauth": ">= 0.8.2" },
+ "repository" : {"type": "git" , "url": "http://github.com/smurthas/fitbit-js.git" },
+ "engines": { "node": ">=0.2.0 <0.5.0" }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.