Cancel
Showing results for 
Search instead for 
Did you mean: 

Google apps script for minute-by-minute data stopped working suddenly

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)

Accepted Solution
0 Votes

@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

Accepted Solution
0 Votes

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.

Accepted Solution
0 Votes

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

 

Accepted Solution
0 Votes

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.

Accepted Solution
0 Votes

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.

 

 

Accepted Solution
0 Votes

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!

 

Accepted Solution
Intraday script doesn't use LOGGABLES. It's only there because I didn't bother to remove it.
Accepted Solution
0 Votes

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.

Accepted Solution
0 Votes

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.

Accepted Solution
0 Votes

@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

Accepted Solution
0 Votes

@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

Accepted Solution
0 Votes

No. I have not been able to download heart rate data.

Accepted Solution
0 Votes
Accepted Solution
0 Votes

Sucks man Smiley Sad 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.

Accepted Solution

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!

Accepted Solution

Thanks for the script kevin__h.
I was anyways planning to do an R analysis, so this script might be handy.

Accepted Solution
Highlighted

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.

Accepted Solution

@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);

Accepted Solution

Y'all are amazing! Thank you everyone for the collaboration and help, especially @gthm , @mattolomew , and @sshi

 

 

Accepted Solution
0 Votes