04-15-2015 15:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

04-15-2015 15:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
I have been using a Google Apps script in a Google Spreadsheet to download minute by minute data. Today I tried to download data for my account and I got "We're sorry, a server error occurred. Please wait a bit and try again" at the top of the spreadsheet. Did something change in the API that might be causing this? It might be something with Google, have no idea how to debug it.
I have another spreadsheet that doesn't do the intraday stuff that's working fine.
This script was based off of things I found online
Here it is https://jsfiddle.net/j72qbe08/
07-23-2015 20:08 - edited 07-23-2015 20:12
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-23-2015 20:08 - edited 07-23-2015 20:12
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
When trying heart rate data request exactly as shown in the example in the API docs I get this:
Request failed for https://api.fitbit.com/1/user/-/activities/heart/date/2015-03-16/1d/1sec/time/12:20/12:45.json returned code 401. Truncated server response: {"errors":[{"errorType":"request","fieldName":"n/a","message":"API client is not authorized to access the resource requested."}]} (use muteHttpExceptions option to examine full response)

07-24-2015 14:03
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-24-2015 14:03
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@sshi i wonder if something is not working with how the Oauth validation is being passed along since the heart rate data actually requires 2.0 whereas the intraday step tracking does not

07-24-2015 15:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-24-2015 15:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Possibly. But I am using the OAuth2 Google script library, following the Fitbit OAuth2 documentation and authorization is quite different from the OAuth1, and I am passing the OAuth2 consumer ID, not the OAuth1 consumer key.

07-24-2015 20:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-24-2015 20:47
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I'm having a slightly different problem. I went through all the setup and authorization, which went fine. Then when I select Download data from the Fitbit menu in the spreadsheet I get an error "TypeError: Cannot read property "dataset" from undefined". The first line of the spreadsheet contains my name then undefined/undefined/undefined. The 2nd line has Date and steps. The rest of the sheet is blank.
Then I noticed there was no opportunity to specify what variables to download. In the insturctions there was a note about specifying loggables in the Setup. But the Setup dialog has no field for loggables - just OAuth 2.0 Client, Consumer secret, project key, start date, end date.
I assume I missed something along the way, though I've gone through this multiple times.
thanks
kevin

07-25-2015 18:28 - edited 07-25-2015 18:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-25-2015 18:28 - edited 07-25-2015 18:32
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Are you talking about downloading step data or heart rate data?
Also, I didn't bother to setup the intraday script as nicely as the interday one. The undefined stuff at the top is normal.
You can't download intraday data unless you have requested and received Partner API access from Fitbit.

07-25-2015 19:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-25-2015 19:57
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
I'd like to be able to get both. I didn't see anywhere in the intraday script that specified what to download. I tried including:
var LOGGABLES = ["steps"];
at the top of the script, and also tried replacing setLoggables(e.parameter.loggables); in the saveSetup function with setLoggables(LOGGABLES);
But it didn't change anything. I still get nothing returned.
I did request (and get) access to the Partner API from Fitbit.

07-25-2015 20:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-25-2015 20:15
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
If its any help, the error is generated at this line in refreshTimeSeries:
var row = o[intradays[activity]]["dataset"];
The error is Cannot read property "dataset" from undefined, which is odd because intradays was defined earlier in the function. Is there a way to debug these scripts?
thanks for your help!
07-26-2015 07:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-26-2015 07:16
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-26-2015 10:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-26-2015 10:00
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Yes, I noticed that last night. Looks like you set the variable to download directly in the refreshTimeSeries function. When I try to download, it does print the header for time and steps, just no data, along with the error I mentioned above.
I assume you've been able to download intraday data?
From the error I get, either intradays is undefined, or maybe activity is out of range? I'm new to google sheets. Is there anyway to set a breakpoint for the debugger? I've tried clicking in the line number. I get a half of a red circle, but it seems to run right past any breakpoint I set. Also, the little debugger icon is grayed out at top of the window.

07-26-2015 12:44
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-26-2015 12:44
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Yes, I have been able to download intraday data, and people have told me they have been able to as well.
You can check the script logs, might show more information (command + enter while in script editor, after launching download). Can add Logger.log(...) statements as needed. Probably is some message from Fitbit's API indicating the problem when the exception is caught. Won't show it in the red popup error.
Google Scripts is basically Javascript by the way. And there are no breakpoints/debugger.

07-27-2015 00:55 - edited 07-27-2015 01:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-27-2015 00:55 - edited 07-27-2015 01:08
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@sshi How can we have access to the resting heart rate in interday.gs (so one resting HH for each day)? I have tried "activities/log/heart" or "activities/log/restingHeartRate" with no success. Am I missing something?
For intraday stuff, I am geting the following error when I hit download data.
TypeError: Cannot read property "dataset" from undefined.
I'm guessing it might be related to the following line of code, but I can be worng!
var row = o[intradays[activity]]["dataset"];
Any suggestions? Thanks for your help.
Abbas

07-27-2015 12:52
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-27-2015 12:52
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
@sshi Were you able to download the Heart Rate data? I could download the other types of intraday datay (steps etc..) but not Heart Rate. I get the same error message like the one you reported earlier.
"API client is not authorized to access the resource requested."
This looks like an authorization issue to me because the resource or path is correct as per the API docs. Could you please let us know if you were able to get the HR data?
Thanks,
Gthm

07-27-2015 13:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-27-2015 13:18
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
No. I have not been able to download heart rate data.

07-27-2015 13:19
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-27-2015 21:11 - edited 07-27-2015 21:12
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-27-2015 21:11 - edited 07-27-2015 21:12
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Sucks man Why do they have to make it so complicated. They could have just given a single button download like they do for normal data. A while back I was using a packet sniffer for the web app to get minute level data but seriously I am wasting much time just to get MY data.
I work for a company that makes a competitive product which is as good as fitbit and I did the mistake of going for fitbit based on asthetics (I do get startes from collegues once in a while). I should have gone for my company product and I would not have had any problem accessing the data.
07-29-2015 16:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-29-2015 16:05
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Yes, this is unnecessarily a PITA.
I was not able to get the google sheets script to work for heart-rate data.
There is an alternative, which I'm now using. There is an R script that scrapes the fitbit data from the cookie generated when you log into fitbit. An advantage is that it doesn't require access to the Partner API. A disadvantage is that you have to install R on your computer, although its free. You can get the script here:
https://github.com/corynissen/fitbitScraper/commit/56fe4434fee30747dd5a6fd7dd52dc142139946b
I'm using it now to access my intraday heart-rate data. Hoping that fitbit will stop trying to figure out how to make another buck on my heart rate measurements and just give us access. Good luck!
07-31-2015 12:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-31-2015 12:26
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
Thanks for the script kevin__h.
I was anyways planning to do an R analysis, so this script might be handy.
07-31-2015 12:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-31-2015 12:36
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
BTW guys, I got the Script working to download second level heart rate. Well its not exactly second level but close (on a typical day I got about data for 20K sec compared to 86.4K seconds in a day).
It turns out that this was an invalid scope/permissions error. Some of the changes I made for the script to be working
1) When requisting Oauth2 the default permission scope does not include heart rate. It pretty much includes everything but heart rate and calories. So for better measure I explicitly granted scope.
In function getFitbitService() { // updated the below line
.setScope('activity heartrate location nutrition profile settings sleep social weight')
2) Update the activities and intradays variables accordingly
var activities = ["activities/heart"];
var intradays = ["activities-heart-intraday"];
3) Request the proper URL for heart rate. The API docs are not clear enought, especially near the URL templates. I got the last template of the API docs working for heart rate.
It was easier for me to hardcode the URL so I just replaced the featch call with this
var result = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/activities/heart/date/2015-07-07/1d/1sec/time/00:00/23:59.json", options);
Once you get this request to work, you can generalize and construct the above request dynamically based on user inputs.
PS: I could not find a way to download intra day heart for multiple days through single call. Looks like the call only supports for a single day (too much data to include multiple days I guess). I am thinking about looping the date range and fetching the details multiple times.
07-31-2015 13:34
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

07-31-2015 13:34
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
- Who Voted for this post?
@gthm - You Rock! This totally worked for me, thank you! Just to flag to others I did have to go through the "authorize" steps through the spreadsheet to authorize the heartrate data.
I was able to pull multiple days by listing inputing them thorugh "Setup" and i used the following call in URLFetchApp
var result = UrlFetchApp.fetch("https://api.fitbit.com/1/user/-/activities/heart/date/" + dateString+ "/1d/1sec/time/00:00/23:59.json", options);
08-01-2015 10:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post

08-01-2015 10:27
- Mark as New
- Bookmark
- Subscribe
- Permalink
- Report this post
Y'all are amazing! Thank you everyone for the collaboration and help, especially @gthm , @mattolomew , and @sshi.

