November 12, 2012

HealthGraphNet - A .NET Library for RunKeeper / HealthGraph

This summer I started running to lose weight. To measure my efforts, I used the RunKeeper iOS app. It's a great product for recording pace, time, distance, location and a whole plethora of other interesting information relevant to your exercise routine.

All of RunKeeper's data gets saved into an API known as the HealthGraph. I was starting to think up some interesting ways to use this information but was dismayed to discover that no one had written a .NET wrapper to integrate with the HealthGraph.

I wanted an easy to use, strongly-typed library, capable of communicating with this REST-based service both synchronously and asynchronously. So I started writing one, called it HealthGraphNet and put it here on GitHub.

The library is comprised of endpoint and model classes. Each endpoint represents a subject within the HealthGraph API (eg. Fitness Activities, Strength Training Activities, Nutrition, Weight, etc) while the models represent the data transfer to and from the service.

Sample use (taken straight out my documentation) in a synchronous situation might look something like this:
//Retrieve the OAuth2 access token.
var tm = new AccessTokenManager(CLIENT_ID, CLIENT_SECRET, REQUEST_URI);
tm.InitAccessToken(AUTH_CODE);

//Retrieve URIs for HealthGraph endpoints.
var userRequest = new UsersEndpoint(tm);
var user = userRequest.GetUser();

//Create a new 30 minute running activity
var newActivity = new FitnessActivitiesNewModel
{
    Type = "Running",
    StartTime = DateTime.Now,
    Duration = 1800,
    Notes = "Around the park."
};
var activitiesRequest = new FitnessActivitiesEndpoint(tm, user);
activitiesRequest.CreateActivity(newActivity);
The folks at RunKeeper were kind enough to list HealthGraphNet on their third-party library page. However, the easiest way to get started is to download the library via Nuget.

I've started to add MonoTouch support but this is still a work in progress. Please feel free to fork HealthGraphNet on GitHub. Pull requests are always welcome!

4 comments:

Eric Dorothy said...

I'm attempting to use the sample you've posted here but I don't know and am unable to figure out where I would get the request uri and auth codes from. Can you point me in the right direction?

Brett Kim said...

This is very good blog which talk about how to use .net NET Library for RunKeeper which is already popular in other platform like IOS.

Anonymous said...

I would also like to use your code but could i get a step by step tutorial how to get all those needed for authentication? Creating an app and so on?

Tron5000 said...

Hi Eric,

These pages might be helpful:
http://developer.runkeeper.com/healthgraph/getting-started

And

https://github.com/Tron5000/HealthGraphNet/blob/master/HealthGraphNet.Samples/Web/Default.aspx.cs

The auth code comes from the authorization endpoint - after a user attempts to log in they are redirected back to their app the auth code is in the url string. This code is then used to create the access token.

I believe the request uri is also the same as redirect uri in the HealthGraph documentation.

Hope this helps!