Monday, July 12, 2010

FatSecret Sharp - C# Nutrition and Exercise Tracking Services


I started looking into FatSecret.com's REST API this weekend after wondering how hard it would be to make a Food Tracking App for Win Phone 7.


My first step was to take a look at their REST API Documentation, which shows a list of their available methods and how to authenticate using OAuth. It turns out, the hardest part of being able to get data from their services is the OAuth stuff.


OAuth Primer



Basically, OAuth works like this; You get a Consumer Key and a Consumer Secret from their API. You use these to "Sign" your requests. The "Signature" is built up using 3 components joined by an "&"; The method you are using to request (POST or GET), the uri of the service (Url Encoded), and the query parameters (ordered alphabetically by key, then value, then Url Encoded).



{HTTPMethod}&{ServiceUrl}&{OrderedParameters}

GET&http://platform.fatsecret.com/rest/server.api&a=foo&oauth_consumer_key=demo&oauth_nonce=abc&oauth_signature_method=HMAC-SHA1&oauth_timestamp=12345678&oauth_version=1.0&z=bar


Final Version:

GET&http%3A%2F%2Fplatform.fatsecret.com%2Frest%2Fserver.api&a%3Dbar%26%26oauth_consumer_key%3Ddemo%26oauth_nonce%3Dabc%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D12345678%26oauth_version%3D1.0%26z%3Dbar


Then you make your request and hope everything matches up on their side.


FatSecret C# Service Wrappers



The end result of digging into the API was a library to wrap the nutrition and exercise services. Now there is an easy way to search for nutrition information in C#. Visit the FatSecretSharp project on codeplex for examples and code. I'm hoping to start making a Win Phone 7 Application for Food Tracking in the next couple weeks.



Now Playing: Weird Al - Fat

1 comment: