Export Google Fit Daily Steps, Weight and Distance to a Google Sheet

Updated on Friday, December 29, 2023

Comments Page 15

Return to post.

Comments

Robert Ellison

Vishal, you shouldn't need to set any of that stuff up. You only need it if you're going to make a public facing app and need to get the consent screen approved by Google and you don't need that for this script.

Vishal

Have question about following -

Open the Google API Console. Create a new project and name it something like 'Google Fit Sheet'. From the Dashboard click Enable APIs and Services and find and select the Fitness API. Then go to Keys and create an OAuth Client ID. You'll be asked to create a consent screen, the only field you need to enter is the product name (i.e. 'My Fit App'). Then choose Web Application as the application type. You need to set the name and the authorized redirect URL. The redirect URL is https://script.google.com/macros/d/{SCRIPTID}/usercallback replacing {SCRIPTID} with the actual Script ID you made a note of above. After adding this make a note of the Client ID and Client Secret.

This seems to be changed when I go to Consent screen in Google API Console. Its asking for more values than explained in above paragraph such as authorized domain, Application Homepage link , Application Privacy Policy link etc. Can you help?

Robert Ellison

Hi James, you shouldn't need to run the authorize script on a schedule. This should be run once when you need to get permissions. If your authorization is expiring run through the steps above to reset everything and try again (reset the script, remove authorization from Google Fit).

James Vondale

This is a great tool, however the 60 minute token is troublesome.

I run these scripts as schedules, however the "Authorize if needed" which fixes the issue has to be run manually. Is there a way to modify this so I can run this without having the click the sidebar?

Leafab

Thank you a lot for this!! I tried measuring the heart rate too, but I can not seem to find the data source that goes with com.google.heart_rate.bpm. From your code I just edited a few things:

var request = {

"aggregateBy": [

{

"dataTypeName": "com.google.step_count.delta",

"dataSourceId": "derived: com. google.step_count.delta:com. google. android.gms:estimated_steps"

},

{

"dataTypeName": "com.google.distance.delta",

"dataSourceId": "derived: com. google.distance.delta:com. google. android.gms:merge_distance_delta"

},

{

"dataTypeName": "com.google.heart_rate.bpm",

"dataSourceId": "derived: com. google.heart_rate.bpm:com. google. android.gms:{I have no idea what to put here}"

}

],

and I added some other things here:

var steps = -1;

var distance = -1;

var heart = -1;

if (json.bucket[b].dataset[0].point.length > 0) {

steps = json.bucket[b].dataset[0].point[0].value[0].intVal;

}

if (json.bucket[b].dataset[1].point.length > 0) {

distance = json.bucket[b].dataset[1].point[0].value[0].fpVal;

}

if (json.bucket[b].dataset[2].point.length > 0) {

distance = json.bucket[b].dataset[1].point[0].value[0].fpVal;

}

sheet.appendRow([bucketDate,

steps == -1 ? ' ' : steps,

distance == -1 ? ' ' : distance,

heart == -1 ? ' ' : heart]);

}

}

If I find anything I'll post another comment. I also wanted to help Mark, on november 11 he asked if anyone could share the code for measuring the heart rate.

Alex

Thank you one more time, Robert. The problem is solved somehow. :) Since Im getting constant errors in downloading Daily Summaries from Google Takeout yor script is very useful for me.

Robert Ellison

Chi, Alex, not sure what could be wrong. Try removing auth from the Fit app and the spreadsheet and then reauth both. Alex, check the API is enabled as well. Chi, sometimes the auth screen gives you the option to limit access to a certain amount of time, could that be the issue?

Chi

Hi Robert,

Recently, I keep getting a time-based error every morning. The Error is: Access not granted or expired. I do go in a re-authorize, and then have it Get Metrics and it works. But then the same issue happens the next morning. How do I get it to do what it was doing before? I went back to your instructions, but I still had the issue today.

Alex

Robert, thank you for your work! I did everything according your explanation. When I try to get Metrics, I get red error message "Error: Access not granted or expired.Details". Did it several times but problem is stiil here. Thanx in advance.

regards, Alex

Robert Ellison

Nadezda - most likely it's an authorization problem. Follow the steps above to remove authorization and then re-authorize. If this doesn't clear it check the API explorer link to make sure you can see weight data that way.

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.