How to get step counter data from Google Fitness REST api?

Since I installed the Google Fit app on my Nexus 5, it tracked the number of steps and the time it took to walk. I would like to get this information using the Google Fitness REST api ( docs ), but I cannot figure out how to get any of this data from the REST api.

I used the OAuth 2.0 Playground to successfully list data sources, but none of the examples I tried returned any fitness data. I feel that I need to use something similar to the DataReadRequest from the ( Android SDK ), but I am not building an Android application - I just want to get fitness data already saved by the Google Fit application.

Is it possible to get the data collected by the Google Fit app? If so, how can I read and summarize step counter data using the REST api?

+8
google-api google-fit
source share
3 answers

It turns out that the answer is in docs . Here is the request format.

GET https://www.googleapis.com/fitness/v1/users/{userId}/dataSources/{dataSourceId}/datasets/{datasetId}

Only the {userId} value of me (with authentication) is supported.

Possible values ​​for {dataSourceId} can be obtained by running another query .

The bit that I missed was that {datasetId} is not really an identifier, but actually where you define the length of time you're interested in. The format for this variable {startTime}-{endTime} , where time is in nanoseconds from the era.

+5
source share

I managed to get this working by going through the google php client and noticing that they add a start and end time for the GET request with an additional 0 - nine infact.

Use the same GET request format as indicated in the answer above:

 https://www.googleapis.com/fitness/v1/users/{userId}/dataSources/{dataSourceId}/datasets/{datasetId} 

Now here is an example with a unix timestamp (the php time() function uses this)

 https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:com.google.android.gms:estimated_steps/datasets/1470475368-1471080168 

This is the answer I get:

 { "minStartTimeNs": "1470475368", "maxEndTimeNs": "1471080168", "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps } 

However, if you add a start and end time with nine 0 that you added to your GET requests, and form your request as follows:

 https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:com.google.android.gms:estimated_steps/datasets/1470475368000000000-1471080168000000000 

This worked - this is the answer I received:

 { "minStartTimeNs": "1470475368000000000", "maxEndTimeNs": "1471080168000000000", "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps", "point": [ { "modifiedTimeMillis": "1470804762704", "startTimeNanos": "1470801347560000000", "endTimeNanos": "1470801347567000000", "value": [ { "intVal": -3 } ], "dataTypeName": "com.google.step_count.delta", "originDataSourceId": "raw:com.google.step_count.delta:com.dsi.ant.plugins.antplus:AntPlus.0.124" }, 

The answer is much longer, but I truncated it for the sake of this post. Therefore, when passing your datasets parameter to the request:

1470475368-1471080168 will not work, but 1470475368000000000-1471080168000000000 will.

It helped me, hope it helps someone!

+2
source share

I tried the mail method with the URL below and body. This will work, please also check the inline comments.

Use URL: https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate Method: POST Body:

  { "aggregateBy": [{ "dataTypeName": "com.google.step_count.delta", "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps" }], "bucketByTime": { "durationMillis": 86400000 }, // This is 24 hours "startTimeMillis": 1504137600000, //start time "endTimeMillis": 1504310400000 // End Time } 
0
source share

All Articles