{"id":18,"date":"2014-03-24T01:47:03","date_gmt":"2014-03-24T01:47:03","guid":{"rendered":"http:\/\/p337.x10host.com\/?p=18"},"modified":"2016-01-14T01:40:10","modified_gmt":"2016-01-14T01:40:10","slug":"my-struggle-with-fitbit-api","status":"publish","type":"post","link":"https:\/\/p337.info\/blog\/my-struggle-with-fitbit-api\/","title":{"rendered":"My Struggle with Fitbit API"},"content":{"rendered":"<p><strong>EDIT: This is no longer the way I get fitbit steps in my website,<\/strong><\/p>\n<p><strong>Please see this new post on how I currently get data on my wesbite<\/strong><\/p>\n<p><strong><a href=\"http:\/\/p337.info\/blog\/?p=88\">http:\/\/p337.info\/blog\/?p=88<\/a><\/strong><\/p>\n<p>Below is the old post that serves historical purposes only.<\/p>\n<p>Ive had a <a href=\"https:\/\/www.fitbit.com\/au\/flex\" target=\"_blank\">Fitbit Flex<\/a> for roughly 3 months now, and enjoying every step and achievement that comes along with the band, and the motivation provided by the web dashboard.<\/p>\n<p>However last month I had decided to give my website a complete redesign, and with my new wristband, I felt that I had the ability to display some personalized dynamic content in the form of my daily step total would be awsome, as I had done a similar things when displaying my &#8220;Last online&#8221; status with the Steam Web API and PHP. Boy was I in for a surprise.<\/p>\n<p>The process for the Fitbit API was immensely more complicated than that of the Steam Web API, Using an authentication process called OAuth, Essentially the process you see when you &#8220;Login with Facebook&#8221; on several websites, meaning that your PHP application needs to send the users attention onto Fitbit to authenticate the access to their data.<\/p>\n<p>Not to mention, this required me to install &#8220;Oauth&#8221; PERL script onto my Website, Which I could not do due to the limitations set by my free web hosting setup. This had me trawling through countless 3rd party OAuth PHP Libraries over the last 3 weeks, Which I understood barely any of.<\/p>\n<p>Eventually I found an Oauth setup utilising Google Spreadsheets and Google Scripts, which allowed me to work around my free web-hosting limitations; technical steps being outlined below<\/p>\n<p>If you&#8217;re following everything I&#8217;ve said up until now, you&#8217;re most likely familiar with API&#8217;s and more specifically Want to actually use it. Please note: You will also need an understanding\/access to a PHP capable server for the implementation of this.<\/p>\n<h3>What to actually do<\/h3>\n<p><strong>Step 1<\/strong>: Go and get a API key from their website,<br \/>\n(recommending you get a &#8220;Browser&#8221; and &#8220;Read only&#8221; API key &#8211; See more at <a title=\"WikiHow\" href=\"http:\/\/www.wikihow.com\/Fitbit-Api-Key\" target=\"_blank\">wikihow<\/a>)<\/p>\n<p><strong>Ste<\/strong><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" src=\"http:\/\/i.imgur.com\/UvaFfrA.png\" alt=\"\" width=\"144\" height=\"278\" \/><strong>p 2<\/strong>: Login to your Google Drive Account, (as we will be using a Google Script to retrieve Fitbit data for us) and create a new spreadsheet.<\/p>\n<p><strong>Step 3<\/strong>: Once you&#8217;re in your new spreadsheet, we will need to open up the Gogole Script Editor (This will open up in a new browser tab)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignright\" src=\"http:\/\/i.imgur.com\/Huk5XLp.png\" alt=\"\" width=\"380\" height=\"187\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Note: Once in the script editor; a &#8220;Blank project&#8221; will be fine to create if you haven&#8217;t ventured into script editor before)<\/p>\n<p><strong>Step 4<\/strong>: I used the script from the <a href=\"https:\/\/github.com\/loghound\/Fitbit-for-Google-App-Script\" target=\"_blank\">following GitHub repository<\/a> (or additionally copy <a href=\"https:\/\/github.com\/loghound\/Fitbit-for-Google-App-Script\/blob\/master\/fitbit.js\">fitbit.js from here<\/a>) and paste it in a new Blank Google script (remove any code in the blank project you just created), and paste Fitbit.js into the code area.<\/p>\n<p><strong>Step 5<\/strong>: Select and Run &#8220;renderFitbitConfigurationDialog&#8221; from the drop down menu on the top toolbar, and press the button with the Play symbol.<\/p>\n<p>&nbsp;<\/p>\n<p>Some pop-up windows for google will open; Click accept or next on all of the authentication steps to allow the script access to your newly made Google spreadsheet<\/p>\n<p>Swap browser tabs back into your Spreadsheet; you should see a configure window shown below,<\/p>\n<p>&nbsp;<\/p>\n<p>After entering your Consumer and Consumer Secret key like the above screenshot,<br \/>\nMake sure everything else is as you desire, and select &#8220;Save Configuration&#8221;<\/p>\n<p>Go back into script editor and run the &#8220;Authorize&#8221; Function<\/p>\n<p>Then proceed to run the &#8220;refreshTimeSeries&#8221; function to populate your data<\/p>\n<p>Swap back to your spreadsheet, and we should see the below setup.<br \/>\nCell B3 is the value were interested in as it displays our daily total (and gets replaced each day)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 6<\/strong>: We need to make sure that our spreadsheet stays active and our steps get updated regularly, so head back into the Google Script Editor, and click &#8220;Current Project&#8217;s triggers&#8221; button<\/p>\n<p>&nbsp;<\/p>\n<p>My triggers look like this<\/p>\n<p>&nbsp;<\/p>\n<p>(This will update my spreadsheet from the Fitbit site every 30 minutes)<\/p>\n<p><strong>Step 7<\/strong>:<strong> Getting a cell to display on our website<\/strong><br \/>\nHeres the part where we need to get our value out of the spreadsheet,<br \/>\nWhilst on our spreadsheet = Under &#8220;File&gt; Publish to web&#8221;, we can extract a cell in a webpage.<\/p>\n<p>&nbsp;<\/p>\n<p>Copy your URL seen above, we will need it for our PHP manipulation.<br \/>\n(Open the URL in a new browser window if you want to check it is the right cell)<\/p>\n<p><strong>Step 8<\/strong>: Create a new php function or page, and use some basic sub-stringing to get your &#8220;B3 Cell&#8221; in a form you can use!<\/p>\n<p>function printFitbitSteps() {<br \/>\n$content = file_get_contents(&#8220;PUBLIC URL FOR GOOGLE SPREADSHEET CELL B3&#8221;);<br \/>\n$content = substr($content, strpos($content, &#8220;&lt;td\u00a0 class=&#8217;s0&#8242;&gt;&#8221;)+16);<br \/>\n$steps = substr($content, 0, strpos($content, &#8220;&lt;\/tr&gt;&lt;\/table&gt;&lt;\/div&gt;&#8221;));<br \/>\necho $steps;<br \/>\n}<\/p>\n<p>The above code reads the HTML given to us in the URL as a variable, and splices it with sub-stringing so that only our step total is left over!<\/p>\n<p>You should now be done,<br \/>\nThis assuming you can utilize PHP on your website, and have a knowledge of how it works, but the basic steps are in those lines of code above. Its probably considered dirty coding, but it works for me.<\/p>\n<p>Here is the footer from <a title=\"My Website\" href=\"http:\/\/petercormick.com\/\" target=\"_blank\">my website<\/a>, focusing on the &#8220;Steps today&#8221; value.<\/p>\n<p>Hopefully this can help someone out!!<\/p>\n<p>References:<br \/>\n<a href=\"http:\/\/quantifiedself.com\/2013\/02\/how-to-download-fitbit-data-using-google-spreadsheets\/\" target=\"_blank\">http:\/\/quantifiedself.com\/2013\/02\/how-to-download-fitbit-data-using-google-spreadsheets\/<\/a><br \/>\n<a href=\"http:\/\/quantifiedself.com\/2013\/02\/how-to-download-fitbit-data-using-google-spreadsheets\/\" target=\"_blank\">https:\/\/github.com\/loghound\/Fitbit-for-Google-App-Script<\/a><\/p>\n<p>Special Thanks to Manuel Lemos who put in heaps of effort to try and teach me his Oauth PHP Library (I just wasnt smart enough)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>EDIT: This is no longer the way I get fitbit steps in my website, Please see this new post on how I currently get data on my wesbite http:\/\/p337.info\/blog\/?p=88 Below is the old post that serves historical purposes only. Ive had a Fitbit Flex for roughly 3 months now, and enjoying every step and achievement [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"quote","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/posts\/18"}],"collection":[{"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":3,"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions\/96"}],"wp:attachment":[{"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p337.info\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}