Giter Club home page Giter Club logo

googlefitbit's Introduction

googlefitbit

Google scripts for Fitbit step data / heart rate interday / intraday download (other data may work with modification, but this script does not intend to support other endpoints at this time. You are welcome to submit PRs to add additional functionality or fix problems. There is some code for other loggables in there but that was copied over from an older version of the script.) You are free to use, duplicate, and/or modify these scripts for any (non-evil) purpose.

These scripts are provided as-is and are not being actively maintained. PRs with fixes and new additions are welcome.

Important Notes: If you run into trouble, please double check the steps carefully and check the error log in the script editor before raising issues or seeking help. These scripts are tricky to set up and they are prone to break.

If you are seeing this error "The app you're trying to connect did not provide valid information to Fitbit. Please report this issue to them."

And "Developer information: invalidrequest - Invalid redirecturi parameter value"

That means you did not follow the instructions correctly.

interday.gs

Download step data, one row per day, from a start day to the present. Make sure not to set the start day too far in the past.

Parts of this script are based on work from the following sources. The main difference in my version is that I use Fitbit's new OAuth2 and Google's OAuth2 instead of OAuthConfig which is deprecated.

Setting up:

  1. Create a new Google spreadsheet (do not try to re-use one with the old version of the script), click Tools > Script editor... then copy and paste the contents of the interday.gs file (see above) into the script editor and save. Return to the spreadsheet and refresh the page (Note: actually click the refresh button or select it from the menu; the keyboard shortcut is overriden on Google Sheets, at least in Google Chrome). A couple seconds after the page reloads you should see a "Fitbit" menu at the top.

  2. Add the Oauth2 Google Script library to your project by clicking Resources > Libraries... (menus inside the script editor). Then search for the OAuth2.0 library by typing in the Script ID "MswhXl8fVhTFUH_Q3UOJbXvxhMjh3Sh48" and hitting Select. More info on that library on its Github page. Select the latest version and click Save. (Note: this library is in active development and newer versions may no longer work with the current version of the scripts in this repository — version 19 should work for now).

  3. Find your project's key by clicking on the 'Fitbit' menu inside your spreadsheet and clicking 'Setup'. A popup window may appear asking you to Authorize your application. Click 'Continue', sign into / select the Google account you want to authorize the application on (doesn't really matter which account, this has nothing to do with your Fitbit account). Then it will show you the permissions the application is requesting, click 'Allow'. Then the 'Setup Fitbit Download' panel should appear. Copy the text adjacent to 'Project Key' in the popup. You will need that in subsequent steps.

  4. Go to https://dev.fitbit.com/apps and log in. If you haven't already, register a new app by clicking at the top right.

  • For OAuth 2.0 Application Type select 'Server' [Fitbit now has a 'Personal' option here which will give you access to intraday data but will only allow you to authorize a single personal account; more info here].

  • Default access type Read only.

  • Other fields at top: Application Name, Description, Application Website, Organization, Organization Website put whatever you want, just need to put something.

  • Add the following inside the callback url box:

    https://script.google.com/macros/d/YOUR_PROJECT_KEY/usercallback

    (Replace YOUR_PROJECT_KEY with the project key you got from step 3).

  • Agree to the terms, and tap register. Then you will be directed to a page with credentials. Keep your 'OAuth 2.0 Client ID' and your 'Client Secret' keys handy.

  1. Inside your spreadsheet (not the script editor; refresh if the Fitbit menu isn't visible at the top), select 'Setup' from the 'Fitbit' menu. In the appropriate fields in the 'Setup Fitbit Download' panel copy in your 'OAuth 2.0 Client ID', 'Client Secret' from the previous step, and your project key from step 3. Select 'activities/logs/steps' from the 'Data elements to download' list, and enter a start/end date (defaults to current day). If you try to download more than a couple years worth of data, Fitbit's API will not like you. Then click 'Save Setup' and the panel will disappear.

  2. In your spreadsheet, click 'Authorize' from the Fitbit menu and a sidebar will show up on the right. Click the word 'Authorize' in the sidebar. A page will open up with the Fitbit login page. Log in to the Fitbit account you would like to download data from in the new window, authorize the application by clicking 'Allow', and then close the tab when it says "Success you can close this tab"

  3. Back in your spreadhseet, hit Sync, and after a few moments the data should load in.

intraday.gs

Download minute-by-minute step data.

Similar setup to interday, except you need to contact Fitbit to request access to intraday data. Also the menu names in the spreadsheet will be slightly different. If you're just developing for a single personal Fitbit account, you can also use the new Personal application type instead of Server, which you can get intraday data with. More information here.

Intraday script is based on this post.

Note: if you want to get heart rate data follow these additional steps, courtesy of gthm on the Fitbit forum

  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')

  1. Update the activities and intradays variables accordingly

var activities = ["activities/heart"]; var intradays = ["activities-heart-intraday"];

  1. 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.

NOTE: I could not find a way to download intraday 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.

I have also uploaded heartrate.gs which is a rough version of a script to download heart rate data.

intraday_all.gs

See the instructions from intraday.gs

This version was created 2017-03-08. It supports downloading steps, calories, heartrate and other intraday data into one sheet. Time interval is by design one minute and cannot be changed with this version.

Configure the sheet name where you want the data to be saved inside the script. By default it is "Sheet1" and if you create a sheet with that name no script changes are needed.

This version retrieves one day per execution so no range can be specified, only a singular date.

Here you can see an example of the data it produces and some example charts: https://docs.google.com/spreadsheets/d/1chREgu98XGocOM8mHyTvbUMlKG_-NfFXsLYHvwq9Dv8/edit#gid=1507130755

googlefitbit's People

Contributors

dsoim avatar erramirez avatar iziki avatar simonbromberg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

googlefitbit's Issues

Resting HR Could be added to interday.gs

Note I have no idea what I'm doing on github so there's probably a not terrible way to document this correctly. Sorry.

The way I did this might be a little hacky, but it's definitely possible without too much trouble. It might be worth adding this - I know I was very interested in having this included, in addition to the other columns already there.

  1. Add "activities/heart" to LOGGABLES
  2. Include heartrate in setscope
  3. Add conditional within sync data population to check title != "heart"
    If true -> cell.offset(index, !dateColDone + activity * 1.0).setValue(val["value"]); (no change)
    If false -> cell.offset(index, !dateColDone + activity * 1.0).setValue(val["value"]["restingHeartRate"]);

dataset from undefined

Hello,
I tried intraday and interday and they worked very weel but whenI run the intraday_all I got this message "TypeError: Unable to read dataset property from undefined" any idea ?
Thanks by advance

intraday_all for yesterday

Hi!
First of all, THANK YOU very much for publishing these scripts, I wouldn't have been able to create them myself :)

I created a script to generate a new Google spreadsheet everyday with the data provided by intraday_all.gs. For it to work, I would need the intraday_all script to fetch yesterday's data, not today's (else I would have to run my script at exactly 11:59pm every day, and google triggers don't allow to be that precise). I couldn't find what I should change in the code to override the "today" from the setup form.
Any help would be greatly appreciated.
Thanks!

Code 401 error

When sync ended, this error occurs.(interday)

https://api.fitbit.com/1/user/-/profile.json에 대한 요청이 실패하여 코드 401이(가) 반환되었습니다(Request failed. Code 401 returned). 서버 응답이 잘림: {"errors":[{"errorType":"authorization","fieldName":"authorization","message":"This application does not have permission to access profile data."}]...(전체 응답을 확인하려면 muteHttpExceptions 옵션을 사용하세요.)

Error

Hi Simon,
Thanks for the script. On the very last stage when I try to authorize I get the following error:

Error retrieving token: 401 (line 305, file "Service", project "OAuth2")

What should I try to do?

Many thanks,
Kishan

Data downloading error.

This errors occurs.

Error downloading foods/log/caloriesIn
Error downloading sleep/timeInBed
Error downloading sleep/minutesAsleep
Error downloading sleep/minutesAwake
Error downloading sleep/awakeningsCount
Error downloading body/weight
Error downloading body/bmi
Error downloading body/fat

and result is this.
2015-10-22 11 57 39

Can't get Intraday data

I have followed all the instructions and managed to get interday data to work. I'm trying to go through and apply the same steps to the intraday scripts - I've tired both. The issue I'm having is that I can't get the 'Sync' option to show up for the last steps. I have no idea where I'm going wrong but noticed on these script there is no mention of sync function.

Hope you can help, thanks!

Error downloading activities/log/steps

Setup my Google Spreadsheet, everything got authorized. Go to Sync under the Fitbit menu drop down, get a pop up with the error that says the following - "Error downloading activities/log/steps". That's all I get. No numbers, no codes. The screen shot is attached.

I am using interday.gs and I had to use the Script ID to authorize instead of the project key

snip_20170804085120

Redirect URI mismatch error when authorizing Fitbit application

A couple of users have pointed out this error after authorizing the Fitbit application.

Error retrieving token: 400: {"errors":[{"errorType":"invalid_request","message":"Redirect_uri mismatch: https://script.google.com/macros/d/YOUR_PROJECT_KEY/usercallback. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false,"granted_time":1465533824} (line 402, file "Service", project "OAuth2")

Looking into it. Anyone with insight please feel free to comment here.

intraday HR for multiple days

Hi
great code - thanks a lot - runs very smoothly.
Would i be possible to get a function to retrieve intraday HR over a period of time when you have some time?
That would be most useful.
Thanks

Heartrate.gs OAuth2 Reference Error

When running the Heartrate.gs in Google Script Editor, I get a ReferenceError: "OAuth2" is not defined when running the Authorize/Download Data Fitbit function.

Under the script editor, I get this error message:

error message

Any help would be greatly appreciated!!!

Thanks,
Jim

This app cannot be verified

In step 2 after downloading OAuth2 library newest version #32 1.32.0, selecting fitbit setup it runs the script but needs permission to do so. I continue to select my gmail account and it responds "This app isn't verified". In addition in the script editor I select file and then project properties and the project key "MVEEoz0tJovApU8EsAUDRv2GcTaOpszs" is (Deprecated). How do you get auhorization for the app?

TypeError on FitBit>Download Data

First, I'm assuming Step 7 should say "Download Data" instead of "Sync", please correct me if I'm wrong.

After following steps 1-6 under "Setting Up" for interday.gs, I attempted step 7 and received the following error in a red box at the top of the spreadsheet:
TypeError: Cannot call method "getContentText" of undefined.

I can see that the following changes were made to the spreadsheet when clicking Download Data, even though there was an error:

  1. Cell A1 is populated with my name and a note with my date of birth.
  2. Cell A2 is populated with "undefined/undefined/undefined"
  3. Rows 1 and 2 were "frozen".

Do you have any suggestions? Is it possible that some new steps are required due to possible changes on FitBit's end?

Also, when I go to the script editor and click Run>setup and then click the red lightbulb labeled "Execution Hints", I get a yellow popup with the text below. I'm not sure if this is a FitBit-specific issue?

UiApp API is deprecated.
File: Code Line: 134
FlowPanel API is deprecated.
File: Code Line: 187
Grid API is deprecated.
File: Code Line: 170
Button API is deprecated.
File: Code Line: 190
ScriptProperties API is deprecated.
File: Code Line: 47
Label API is deprecated.
File: Code Line: 190
ServerHandler API is deprecated.
File: Code Line: 184
UiInstance API is deprecated.
File: Code Line: 134
TextBox API is deprecated.
File: Code Line: 139

intraday_all.gs: "dataset" from undefined

Hi,

I was following lilmo feedback on the intraday_all.gs but I am getting the following error:

[17-11-12 00:57:15:127 GMT] Sheet.getRange([a1]) [0.058 seconds]
[17-11-12 00:57:15:171 GMT] Range.setValue([Time]) [0.044 seconds]
[17-11-12 00:57:15:427 GMT] UrlFetchApp.fetch([https://api.fitbit.com/1/user/-/activities/steps/date/2017-11-11/1d.json, {headers={Authorization=Bearer eyJhbGcuednmkwejd92309.eyJzdWIiOiI2kldwjdo3iNLRlgiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3Nf, =}}]...) [0.254 seconds]
[17-11-12 00:57:15:428 GMT] HTTPResponse.getContentText() [0 seconds]
[17-11-12 00:57:15:429 GMT] Range.offset([0, 1]) [0 seconds]
[17-11-12 00:57:15:430 GMT] Range.setValue([steps]) [0.001 seconds]
[17-11-12 00:57:15:495 GMT] Execution failed: TypeError: Cannot read property "dataset" from undefined. (line 332, file "Code") [0.696 seconds total runtime]

The interday.gs works fine.

Many Thanks,
Eugen

TypeError: Cannot call method "getContentText" of undefined.

Hi,
I have done the same that you mentioned in the readme when i hit sync i am getting this error .
Can you please help me out on this ?

TypeError: Cannot call method "getContentText" of undefined.

Sheet last synced: Thu Sep 17 2015 16:44:13 GMT-0700 (PDT)
Battery
Last Sync
Date

State token invalid or expired

I'm going through the process of loading this code and everything goes as expected until I go to the authorize part. It goes to the fitbit page with 'heart rate', 'activity and exercise', and 'profile' where you can deny or allow. Upon clicking 'allow'. This error appears:
"The state token is invalid or has expired. Please try again."

ScriptProperties is deprecated

Hi Simon

I opened an issue a little bit ago which I thought was resolved by using the correct script. However, I found that the interday script uses ScriptProperties, which is deprecated and not working. I modified the script to use PropertiesService - I'm have a somewhat limited understanding of coding, but I modified each function which included "ScriptProperties.setProperty" or "ScriptProperties.getProperty" by doing the following:

  1. Add var userProperties = PropertiesService.getUserProperties(); as the first line of the function
  2. Replace all instances of ScriptProperties.setProperty with userProperties.setProperty
  3. Replace all instances of ScriptProperties.getProperty with userProperties.getProperty

I think that adding var userProperties = PropertiesService.getUserProperties(); anywhere before line 44 would have also worked instead of adding it as the first line of any function using ScriptProperties, but I didn't test this.

Additional information regarding Sync errors on some Fitbit data

These are the Loggables that are having issues:
foods/log/claoriesIn
sleep/timeInBed
sleep/minutesAsleep
sleep/minutesAwake
sleep/awakeningsCount
body/weight
body/bmi
body/fat

I noticed that all the Loggables with activities/log work and foods,sleep and body do not. Sorry I am not experienced enough to figure out why and how to fix the Logger.log function or maybe a better way to define the var. So I edited the var LOGGABLES section to remove those that are not working , add floors and put the activities in the order you would see them if you did a direct download from Fitbit.

// Default loggable resources (from Fitbit API docs).
var LOGGABLES = ["activities/log/calories", "activities/log/steps", "activities/log/distance","activities/log/floors","activities/log/minutesSedentary","activities/log/minutesLightlyActive","activities/log/minutesFairlyActive","activities/log/minutesVeryActive","activities/log/activityCalories",];

New V8 runtime Error and Data Download Error with workarounds (from a newb)

I have very limited coding experience, and recently went to use the original code by @simonbromberg. I ran into 3 issues that were not addressed in the initial setup instructions, last updated in 2018. I thought I would put them here for other newbs who are just trying to get their Fitbit data.

The first issue is that Google's script editor has been switched over to V8 runtime; which seems to have happened around Feb 2020. This meant that I needed to use the latest version (38) of the OAuth2.0 library, not version 19 as was mentioned.

I needed to tweak the First Date in the gs script to get the data. I had to set the 'getFirstDate' function to the day I was pulling info from. It is defaulted to 01/01/2012 and would not save as any other date for me. I just changed the date in the gs code to be the current date and was able to pull this info when I went to Download Data later.

The remaining issues I ran into have already been addressed in @lordneeko's post and @rubenflamshepherd's post.


The second issue was that the UI has been deprecated. This was well addressed by @lordneeko in his PR#37 post. You just use the Setup.html code and save it as Setup in the script editor and then use the respective gs codes he has listed (intraday.gs, heartrate.gs, etc.) as you would have previously. @pjfarr explained how to do this well in his reply on @lordneeko 's post.

The final issue was that the Project Key for the callback URL did not work for me and I instead used the script ID setup discussed in this post by Jozef Jarosciak and by @rubenflamshepherd's post.


I have only used the heartrate.gs file so far, and it worked. Adjusting for these 3 issues and the date seemed to do the trick. Jozef Jarosciak's instructions were very helpful for someone lacking experience.

**I don't know a lot of the jargon, so I apologize if some of my terminology was off.

script unable to download some data

The script is throwing up an error dialog for the following:

caloriesin
timeinbed
minutesasleep
minutesawake
awakeningscount
weight
bmi
fat

everytime it throws up an error about a specific metric, it duplicates the previous metric.

Error: Access not granted or expired

I've followed the instructions in Interday.gs without any issues, however when syncing at the end of the instructions, the error in Google spreadsheets comes up in a red box: "Error: Access not granted or expired. Details Dismiss".
Clicking 'Details' returns the message box "Error: Access not granted or expired.".
Clicking on the 'Fitbit' Menu and 'Authorize' brings up a side box on the right with a link which leads to 'fitbit.com/oauth2/authorize?client_id=....' with the following error message:
"The app you're trying to connect did not provide valid information to Fitbit. Please report this issue to them.

Developer information: invalid_request - Invalid redirect_uri parameter value "
I have the latest library resource 'OAuth2' available which is currently at version 26.

Problems with access and data loading

Dear Simon,
I am a psychiatrist with only very small knowledge in programming. For a study I want to get the intraday sleep data out of the Fitbit One. I requested a partner API as recommend and got access. I was very lucky to find your intraday.gs, but after several days of trying and getting forward slowly, I now hit a wall, I cannot go through on my own due to my lack of knowledge at these things. I would really appreciate your help.
My problem start and step 6:

In your spreadsheet, click Authorize from the Fitbit menu and a sidebar will show up. Click the link in the >sidebar, log in to Fitbit in the new window, authorize the application, and then close the tab when it says >"Success you can close this tab"

I do not find a authorize command, but I clicked the showSidebar, then I can authorize. In the new window Fitbit asks me to accept or deny the ability to access the certain data in my account. However, when I click accept, the message "Success you can close this tab" does not appear, but a blank page. When I then go to step 7:

Back in your spreadhseet, hit Sync, and after a few moments the data should load in.
There is no Sync command but a refreshTimeSeries command. However when I click it, a message appears saying: "Access not granted or expired". I thought the Fitbit API team provided access to me...

If you have an ideas to my problem I would really appreciate your input. Coffee is for sure ;-)

Best wishes,
Laura

Intraday_all does not Work

By downloading the data, I do get the Error : "Method "getmaxRows" from zero can not be run.
Can anyone help on this?

var doc = SpreadsheetApp.getActiveSpreadsheet();
var sheet = doc.getSheetByName(mySheetName);
var lastrow = sheet.getMaxRows();
sheet.clear();
if (lastrow > 3) { sheet.deleteRows(2, lastrow -2); }
sheet.setFrozenRows(1);

Thank you

Error on Authorization

Everything was working fine until today. Now I get this error when I use the sync option:
image

And when I try to authorize I get:
Data storage error (line 493, file "Service", project "OAuth2")

Any ideas?

invalid_request - Invalid redirect_uri parameter value

Hey guys, I'm getting this error on Step 6 when I try to authorize the app:

The app you're trying to connect did not provide valid information to Fitbit. Please report this issue to them.

Developer information: invalid_request - Invalid redirect_uri parameter value

I was able to bypass this error by selecting OAuth2 version 19 (instead of selecting the latest version) in the resources library in the scripts.

UiApp has been deprecated

When attempting to run the script for a new spreadsheet (or change settings in existing spreadsheet) the following message is now displayed.

"UiApp has been deprecated. Please use HtmlService instead."

Occurs when going to Fitbit menu item and selecting Setup menu choice.

Sleep Stages Request

Hi everyone, not sure if this is the right place to put a feature request, so I'm just going to put it here in issues (feel free to delete this).
https://dev.fitbit.com/docs/sleep/#get-sleep-logs-list
Fitbit just pushed out new sleep stages information. Would it be possible to modify the code here to start pulling this new data in?
Thanks - nick

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.