npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

withings-oauth2

v0.0.9

Published

Withings API for NodeJS

Downloads

22

Readme

withings-oauth2

NPM

Build Status Dependency Status Coverage Status

Withings API library for node.js

Install

$ npm install --save withings-oauth2

Usage

var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
var session = require('express-session');
var Withings = require('../dist/Withings').Withings;

app.use(cookieParser());
app.use(session({secret: 'bigSecret'}));
app.listen(3000);

// OAuth flow
app.get('/', function (req, res) {
    // Create an API client and start authentication via OAuth
    var options = {
        consumerKey: process.env.CONSUMER_KEY,
        consumerSecret: process.env.CONSUMER_SECRET,
        callbackUrl: process.env.CALLBACK_URL
    };
    var client = new Withings(options);

    client.getRequestToken(function (err, token, tokenSecret) {
        if (err) {
            // Throw error
            return;
        }

        req.session.oauth = {
            requestToken: token,
            requestTokenSecret: tokenSecret
        };

        res.redirect(client.authorizeUrl(token, tokenSecret));
    });
});

// On return from the authorization
app.get('/oauth_callback', function (req, res) {
    var verifier = req.query.oauth_verifier;
    var oauthSettings = req.session.oauth;
    var options = {
        consumerKey: config.CONSUMER_KEY,
        consumerSecret: config.CONSUMER_SECRET,
        callbackUrl: config.CALLBACK_URL,
        userID: req.query.userid
    };
    var client = new Withings(options);

    // Request an access token
    client.getAccessToken(oauthSettings.requestToken, oauthSettings.requestTokenSecret, verifier,
        function (err, token, secret) {
            if (err) {
                // Throw error
                return;
            }

            oauthSettings.accessToken = token;
            oauthSettings.accessTokenSecret = secret;

            res.redirect('/activity');
        }
    );
});

// Display today's steps for a user
app.get('/activity/steps', function (req, res) {
    var options = {
        consumerKey: config.CONSUMER_KEY,
        consumerSecret: config.CONSUMER_SECRET,
        accessToken: req.session.oauth.accessToken,
        accessTokenSecret: req.session.oauth.accessTokenSecret,
        userID: req.query.userid
    };
    var client = new Withings(options);

    client.getDailySteps(new Date(), function(err, data) {
        if (err) {
            res.send(err);
        }
        res.json(data);
    });
});

Client API

Activity Measures

Withings.getDailySteps(date, callback)

The date is a Date object, and the callback is of the form function(err, data). The data is the integer number of steps the user has taken today.

Withings.getDailyCalories(date, callback)

The date is a Date object, and the callback is of the form function(err, data). The data is the integer number of calories the user has consumed today.

Body Measures

Body measures return measurement group arrays with the following form. Measures are displayed in scientific notation.

{
    "grpid": <Integer>,
    "date": <UNIX timestamp>,
    "measures": [
       {
           "value": <Integer mantissa>,
           "type": <Ingteger, corresponding to data type>,
           "unit": <Integer exponent>
       }
    ]
}

Withings.getWeightMeasures(startDate, endDate, callback)

The dates are Date objects, and the callback is of the form function(err, data). The data is an array of measurement groups.

Withings.getPulseMeasures(startDate, endDate, callback)

The dates are Date objects, and the callback is of the form function(err, data). The data is an array of measurement groups.

Sleep Summary

Sleep summaries return series arrays with the following form. Durations are displayed in seconds.

{
   "id": <Integer>,
   "startdate": <UNIX timestamp>,
   "enddate": <UNIX timestamp>,
   "date": <YYYY-MM-DD>,
   "data":
   {
     "wakeupduration": <Integer duration>,
     "lightsleepduration": <Integer duration>,
     "deepsleepduration": <Integer duration>,
     "remsleepduration": <Integer duration>,
     "durationtosleep": <Integer duration>,
     "durationtowakeup": <Integer duration>,
     "wakeupcount": <Integer>
    },
    "modified": <UNIX timestamp>
}

Withings.getSleepSummary(startDate, endDate, callback)

The dates are Date objects, and the callback is of the form function(err, data). The data is an array of measurement groups.

Notifications

For notifications requests, parameters should be used as follows:

  • callbackUrl

    • The URL the API notification service will call. This URL will be used as a key whenever one needs to list it or revoke it. WBS API notifications are merely HTTP POST requests to this URL (such as http://www.yourdomain.net/yourCustomApplication.php?userid=123456&startdate=1260350649 &enddate=1260350650&appli=44). Those requests contain startdate and enddate parameters (both are integers in EPOCH format) and the userid it refers to. It is up to the targeted system to issue a measure/getmeas request using both figures to retrieve updated data. The payload for each appli type is as follows:
      • 1 (Body Scale) : userid=123545&startdate=1411002541&enddate=1411002542
      • 4 (Blood pressure monitor) : userid=123545&startdate=1411002541&enddate=1411002542
      • 16 (Withings pulse) : userid=123545&date=2014-06-08
      • 44 (Sleep monitor) : userid=123545&startdate=1411002541&enddate=1411003542
  • comment

    • The comment string is used as a description displayed to the user when presenting the notification setup.
  • appli

    • The value for this parameter is a number, which corresponds to:
      • 1: Weight
      • 4: Heart Rate, Diastolic pressure, Systolic pressure, Oxymetry
      • 16: Activity Measure ( steps, calories, distance, elevation)
      • 44: Sleep

Withings.createNotification(callbackUrl, comment, appli, cb)

A notification lets your system be informed every time new data is available for a user. Withings will call a provided url every time the user syncs its withings device with its account. Returns the status of the POST request.

Withings.getNotification(callbackUrl, appli, cb)

Allows third party applications to check whether the notification service was previously subscribed on a specific user and to retrieve the subscription expiration date. Returns the notification expiration date and comment.

Withings.listNotifications(appli, cb)

List notification configurations for a user. Returns an array of notifications with expiration date and comment.

Withings.revokeNotification(callbackUrl, appli, cb)

Allows third party applications to revoke a previously subscribed notification. This will disable the notification feature between the WBS API and the specified applications for a user. Returns the status of the GET request.

Contributing

Contributions are welcome. See issues here.

Release Notes

See release notes here.

License

Licensed under Apache 2.0.