Intersystems Cache Fitbit Client API Demo

My Fitbit watch is my internet of things.

It keeps track of my steps, bpm, calories burned, activity and stationary periods.

The code attached to this blog is prototype software, but its intention is to not only retrieve your Fitbit data, but to demonstrate the processes and technologies that underpin OAuth 2.0.

More information about the Fitbit API can be found here:
https://dev.fitbit.com/uk

More information about Cache can be found here:
https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GIC

A free version of Cache can be downloaded from here:
http://www.intersystems.com/library/software-downloads/

In order to run the demo, you’ll need to do the following:

1. You’ll need to Register an application using https://dev.fitbit.com/apps/new
Make sure the Callback URL uses port 2000.
For example: http://localhost:2000/csp/pstest/

2. After registering your app, the Fitbit web site will generate a Client ID and Client Secret (used in step 5).

3. Install cURL (if its not installed on your computer).
A copy of cURL for Windows can be downloaded from http://www.confusedbycode.com/curl/.
The official curl web site is https://curl.haxx.se/.

4. Download the code by clicking on the Code link at the bottom of this blog.
Extract the FB.xml file from the MS Word document, and copy it to C:\TEMP\.
Then, in a Cache Terminal:
ZN “USER” Write $SYSTEM.OBJ.Load(“C:\TEMP\FB.xml”,”ck”)

5. Execute the following code in the USER namespace, but replace YOUR-CLIENTID and YOUR-SECRET-QUESTION with your Fitbit credentials.
Set ^Fitbit.Config(“client_id”)=”YOUR-CLIENTID”
Set ^Fitbit.Config(“secret_question”)=”YOUR-SECRET-QUESTION”
Set ^Fitbit.Config(“redirect_uri”)=”http%3A%2F%2F127.0.0.1%3A57728%2Fcsp%2pstest%2″

6. Do ^FB in a Terminal session, which should launch IE and then write out your Fitbit profile in the terminal session.

USER>Do ^FB
Scanning for authorization code
GET /csp/pstest/?code=919056c646d9b5767a0798f6c6fdf5bc11ea89d8 HTTP/1.1
Accept: text/html, application/xhtml+xml, image/jxr, */*
Accept-Language: en-GB,en-US;q=0.7,en;q=0.3
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; LCJB; rv:11.0) like Gecko
Accept-Encoding: gzip, deflate
Host: localhost:2000
Connection: Keep-Alive
Cookie: state-9AD096D1-66A0-49EF-BB58-E1BE668C6139=DEEPSEE%3A; Username=_SYSTEM; nsstate.false.HSACCESS=; state-3013AF90-9846-4700-A77A-5DFF76B22D58=ENS%3A0; state-1F02C113-F836-4C97-A2C3-92FAE6C43E6F=SYSADM%3A0%2C0; state-0C5A7E6C-71EC-4605-9A63-3F4EBC41E404=XENS%3A; state-970DC7BC-04DF-4185-A672-5A744437CBA1=SYSEXP%3A; state-73C156CE-8D33-4FCD-B224-2F5108C53999=ENS%3A0; nsstate.false.HSPD=; state-B2A4AB48-DABD-4297-8ECD-60D4D31DCCB6=ENS%3A0

Paul Simon
Urban Boot (15,000 steps in a day)
15,000 steps in a day
Congrats on earning your first Urban Boot badge!

London Underground (250 lifetime miles)
250 lifetime miles
Whoa! You’ve earned the London Underground badge!

Castle (200 floors in a day)
200 floors in a day
Congrats on earning your first Castle badge!

Hot Air Balloon (2,000 lifetime floors)
2,000 lifetime floors
Yipee! You’ve earned the Hot Air Balloon badge!

Code

Enjoy!

2 Comments

  1. Posted February 25, 2017 at 9:14 pm | Permalink | Reply

    Hi, it would be much better, if you could create GitHub repository with the sources. If you don’t know how to do it, you can read tutorial here https://guides.github.com/activities/hello-world/
    And would be nice if you will write the same article on InterSystems Developer Community Portal, https://community.intersystems.com/

    • Posted February 26, 2017 at 5:21 pm | Permalink | Reply

      Hi, I’ll submit the code to GitHub and post the same article to the ISC developer portal as soon as I get time. Many Thanks.

Leave a comment