I want to get all the activities registered in my google fit account between two dates (automatically registered ones such as walking and the rest) with all their information. I have spent some hours reading the Google Fit Rest API documentation, but I don't understand it well, so I can't obtain the desired data.
I don't know if the documentation of the API is messy, if there are few examples or if I don't understand it as my native language is not English (problably), but I don't understand how to do the calls.
If I am right:
- Google Fit API divides the type of data in 4 types: Sessions, data sources, data sets and data points.
- A session represents one activity.
- One datasource has many datasets and each dataset has many datapoints.
I want to retrieve all my activities between a period of time and with all the data for each activity(steps, calories, speed, heartrate, location, device that recorded the activity) in a Json.
What I have achieved
If I want to retrieve all my activities I can call:
Call
GET https://www.googleapis.com/fitness/v1/users/me/sessions.
- I can use startTime and endTime to limit the sessions to retrieve.
The call response is:
{
"session": [
{
"id": "ef3107ca-a132-43df-ba87-5767bdb2e832",
"name": "nadar",
"description": "",
"startTimeMillis": "1643155200000",
"endTimeMillis": "1643160600000",
"modifiedTimeMillis": "1643195812287",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 82
},
{
"id": "21195d09-bc89-4457-8293-039839808526",
"name": "cicl",
"description": "",
"startTimeMillis": "1643161800000",
"endTimeMillis": "1643163600000",
"modifiedTimeMillis": "1643189389446",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 14
},
{
"id": "be9f60b4-79e9-41d5-bf64-e366b63d86ef",
"name": "Bailar",
"description": "Mal",
"startTimeMillis": "1609647420000",
"endTimeMillis": "1609656420000",
"modifiedTimeMillis": "1643224174323",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 24
},
{
"id": "bef8bda8-b24e-4d04-a22a-86338e6049fe",
"name": "danza",
"description": "",
"startTimeMillis": "1643185566914",
"endTimeMillis": "1643187366914",
"modifiedTimeMillis": "1643188254598",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 24
},
{
"id": "6e72bdbd-4ce2-47cc-b857-4ab3487a6cc7",
"name": "boxeo1",
"description": "",
"startTimeMillis": "1643168460000",
"endTimeMillis": "1643170260000",
"modifiedTimeMillis": "1643220544876",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 20
},
{
"id": "e36f5557-2f76-44d3-a80e-464d5a7f6fb4",
"name": "boxeo2",
"description": "",
"startTimeMillis": "1643212744367",
"endTimeMillis": "1643218144367",
"modifiedTimeMillis": "1643220544876",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 20
},
{
"id": "2625516e274d7f96:activemode:baseball:1643225867610",
"name": "béisbol por la tarde",
"description": "",
"startTimeMillis": "1643225867647",
"endTimeMillis": "1643225875355",
"modifiedTimeMillis": "1643226008584",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 11
},
{
"id": "0f1d829e-22e0-4f79-b23f-04e60af95dbc",
"name": "sdch",
"description": "",
"startTimeMillis": "1643186984290",
"endTimeMillis": "1643188784290",
"modifiedTimeMillis": "1643189389446",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 12
},
...
],
"deletedSession": [],
"nextPageToken": "XmRh-ZWj_CZxgJml3CJjZQ"
}
With that data I can obtain the Google's fit main page activity list data (Except red squared activities and information). I can obtain the start date with "startTimeMillis", the name with "name", the duration substracting "startTimeMillis" and "endTimeMillis".
Authomatic steps activitries are not returned with this GET call and the "heart points" neither. ¿How can I obtain that data?
Then, I want to get all the data for each activity (blue squares data).
To achieve that, I use the following POST call (I have achieved trhat response by test fail, as I don't understand properly each param of the body of the post call):
Call
POST https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
Body
{
"aggregateBy": [
{
"dataTypeName": "com.google.calories.expended",
}, {
"dataTypeName": "com.google.step_count.delta",
},
{
"dataTypeName": "com.google.speed",
},
{
"dataTypeName": "com.google.active_minutes",
},
{
"dataTypeName": "com.google.distance.delta",
},
{
"dataTypeName": "com.google.heart_minutes",
},
],
"startTimeMillis": 1643151600000,
"endTimeMillis": 1643238000000,
bucketBySession: {}
}
What I understand about that body is that I want to retrive data packaged by session (activity) between"startTimeMillis" and "endTimeMillis". So, If two activities of the same type are done in that period, both will appear in the response. For each session (activity) package, I want to retrieve calories, steps, speed, minutes, distance and heart minutes (heart points).
The result is something like:
{
"bucket": [
{
"startTimeMillis": "1643155200000",
"endTimeMillis": "1643160600000",
"dataset": [
{
"dataSourceId": "derived:com.google.calories.expended:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643155200000000000",
"endTimeNanos": "1643160600000000000",
"dataTypeName": "com.google.calories.expended",
"originDataSourceId": "raw:com.google.calories.expended:com.google.android.apps.fitness:user_input",
"value": [
{
"fpVal": 600,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated",
"point": []
},
{
"dataSourceId": "derived:com.google.speed.summary:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643157900000000000",
"endTimeNanos": "1643157900000000000",
"dataTypeName": "com.google.speed.summary",
"originDataSourceId": "raw:com.google.distance.delta:com.google.android.apps.fitness:user_input",
"value": [
{
"fpVal": 0.55555558204650879,
"mapVal": []
},
{
"fpVal": 0.55555558204650879,
"mapVal": []
},
{
"fpVal": 0.55555558204650879,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.active_minutes:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643155200000000000",
"endTimeNanos": "1643160600000000000",
"dataTypeName": "com.google.active_minutes",
"originDataSourceId": "raw:com.google.active_minutes:com.google.android.apps.fitness:user_input",
"value": [
{
"intVal": 90,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.distance.delta:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643155200000000000",
"endTimeNanos": "1643160600000000000",
"dataTypeName": "com.google.distance.delta",
"originDataSourceId": "raw:com.google.distance.delta:com.google.android.apps.fitness:user_input",
"value": [
{
"fpVal": 3000,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.heart_minutes.summary:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643155200000000000",
"endTimeNanos": "1643160600000000000",
"dataTypeName": "com.google.heart_minutes.summary",
"value": [
{
"fpVal": 180,
"mapVal": []
},
{
"intVal": 90,
"mapVal": []
}
]
}
]
}
],
"session": {
"id": "ef3107ca-a132-43df-ba87-5767bdb2e832",
"name": "nadar",
"description": "",
"startTimeMillis": "1643155200000",
"endTimeMillis": "1643160600000",
"modifiedTimeMillis": "1643195812287",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 82
}
},
{
"startTimeMillis": "1643161800000",
"endTimeMillis": "1643163600000",
"dataset": [
{
"dataSourceId": "derived:com.google.calories.expended:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643161800000000000",
"endTimeNanos": "1643163600000000000",
"dataTypeName": "com.google.calories.expended",
"originDataSourceId": "raw:com.google.calories.expended:com.google.android.apps.fitness:user_input",
"value": [
{
"fpVal": 200,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:aggregated",
"point": []
},
{
"dataSourceId": "derived:com.google.speed.summary:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643162700000000000",
"endTimeNanos": "1643162700000000000",
"dataTypeName": "com.google.speed.summary",
"originDataSourceId": "raw:com.google.distance.delta:com.google.android.apps.fitness:user_input",
"value": [
{
"fpVal": 11.111110687255859,
"mapVal": []
},
{
"fpVal": 11.111110687255859,
"mapVal": []
},
{
"fpVal": 11.111110687255859,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.active_minutes:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643161800000000000",
"endTimeNanos": "1643163600000000000",
"dataTypeName": "com.google.active_minutes",
"originDataSourceId": "raw:com.google.active_minutes:com.google.android.apps.fitness:user_input",
"value": [
{
"intVal": 30,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.distance.delta:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643161800000000000",
"endTimeNanos": "1643163600000000000",
"dataTypeName": "com.google.distance.delta",
"originDataSourceId": "raw:com.google.distance.delta:com.google.android.apps.fitness:user_input",
"value": [
{
"fpVal": 20000,
"mapVal": []
}
]
}
]
},
{
"dataSourceId": "derived:com.google.heart_minutes.summary:com.google.android.gms:aggregated",
"point": [
{
"startTimeNanos": "1643161800000000000",
"endTimeNanos": "1643163600000000000",
"dataTypeName": "com.google.heart_minutes.summary",
"value": [
{
"fpVal": 30,
"mapVal": []
},
{
"intVal": 30,
"mapVal": []
}
]
}
]
}
],
"session": {
"id": "21195d09-bc89-4457-8293-039839808526",
"name": "cicl",
"description": "",
"startTimeMillis": "1643161800000",
"endTimeMillis": "1643163600000",
"modifiedTimeMillis": "1643189389446",
"application": {
"packageName": "com.google.android.apps.fitness"
},
"activityType": 14
}
},
...
}
]
}
I don't know if it is the best way to achieve the activities data as I don't understand the API documentation. So, what I am asking is about a propper and a clean way to achieve the information of each activity and the authomatically recorded activities (i don't want the erased ones) and a little about explanation about it. I would also want to get the device that recorded each activity.
More doubts: Which is the difference between a datasourceName and datasourceId? Why is all so confusing?
When I try to use some dataTypeName such as: com.google.activity.exercise, com.google.activity.summary....in the body of my calls I get the following error and I can't understand why.
{
"error": {
"code": 400,
"message": "no default datasource found for: com.google.activity.summary",
"errors": [
{
"message": "no default datasource found for: com.google.activity.summary",
"domain": "global",
"reason": "invalidArgument"
}
],
"status": "INVALID_ARGUMENT"
}
}