Yesterday I decided to create a simple web application that demonstrates how to authorize with the Fitbit API and extract data from your account. You can see the result on my github repository. You can also download a runnable version of my webapp – all you need is Java.
Ok, now let’s get to the details. 🙂
First of all, I didn’t want any bloated framework to create a very simple example application, so I decided to use the Spark framework to build my web application. If you are familiar with frameworks like Sinatra or NodeJS you will feel at home. It’s a very basic framework, and you have to care only about the bare minimum to get a web application running. I’ll use a lot of Spark/Sinatra/Node-lingo now.
Here’s the main part of my application, the routes that are responsible for authorization with Fitbit, retrieving steps data and finally displaying the step count as a graph:
Please note that I left out the error handling and some other things for brevity. If you want to see the full code, go to my github repository.
The first route
/login is responsible to create the redirect to the authorization page if no credentials are found for the current user. After authorization the user is redirected back to the application where she ends up in the
/login route again, this time with valid authorization query parameters (if parameter
completeAuthorization is found, Fitbit completed the authorization for the user successfully). I’m then storing the credentials in the Fitbit API client
fitbit, putting the
user and the
client into the session and redirect to the route
/steps then makes a call to the Fitbit API and retrieve the steps for the last 7 days. The method
loadData(...) uses the fitbit client to get a TimeSeries of
TimeSeriesResourceType.STEPS for a period of
If you have any questions, feel free to leave a comment or contact me on Google+.