15

Since I installed the Google Fit app on my Nexus 5 it has been tracking my step count and time spent walking. I'd like to retrieve this info via the Google Fitness REST api (docs) but I can't work out how to get any of that data from the REST api.

I've used the OAuth 2.0 playground to successfully list dataSources but none of the examples I have tried have returned any fitness data whatsoever. I feel like I need to use something similar to a DataReadRequest from the (Android SDK) but I'm not building an Android app -- I just want to access fitness data already stored by the Google Fit app.

Is it even possible to get the data gathered by the Google Fit app? If so, how can I read and aggregate step count data using the REST api?

3 Answers3

9

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

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

The only supported {userId} value is me (with authentication).

Possible values for {dataSourceId} are avaiable by running a different request.

The bit I missed was that {datasetId} is not really an ID, but actually where you define the timespan in which you are interested. The format for that variable is {startTime}-{endTime} where the times are in nanoseconds since the epoch.

  • 1
    Could you tell me the exact query that you have used for getting the step count? I'm trying it with something like "https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta/datasets/1419535318000000-1419335318000000" but I dont get a "readeable" output. Thanks in advance – m_jero Dec 25 '14 at 19:42
  • I get this same thing as @m_jero – RuSs Feb 06 '15 at 03:35
  • Hi @Chornsby. just wondering what you got back in your response. I put in this:https://www.googleapis.com/fitness/v1/users/me/dataSources/derive_step_cadence<-raw:com.google.step_count.cumulative:LGE:Nexus 5:bc5f1b97:Step Counter/datasets/8587796212108459505-8587784116108409439 – RuSs Feb 06 '15 at 04:22
  • I had the most luck with `derived:com.google.step_count.delta:com.google.android.gms:estimated_steps` as the `dataSourceId` – azurewraith Feb 14 '15 at 05:38
  • 3
    Very Bad api support. – Devaroop Feb 18 '15 at 10:28
  • REST API does not give aggregated values for day like andriod api – AdityaKapreShrewsburyBoston Jul 16 '15 at 14:23
  • 2
    i was working on this rest api but when i put this query it doesnt return any integer value instead it just gives me a json content with start and end timings and datastream id which i had put in the query.. any idea what to do?? query: https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.calories.expended:com.google.android.gms:merge_calories_expended/datasets/1461628800-1461864195 response : { "minStartTimeNs": "1461628800", "maxEndTimeNs": "1461864195", "dataSourceId": "derived:com.google.calories.expended:com.google.android.gms:merge_calories_expended" } – Shravan Apr 28 '16 at 17:36
  • 1
    Did anyone ever find a solution to this? I am stuck with on what Shravan said, any api call I make just tells me the start and endtimes I supplied in the GET request. – Countach Aug 13 '16 at 09:35
  • It is possible to call API for iOS App ? – Govaadiyo May 25 '18 at 12:17
9

I was able to get this working by going through the google php client and noticed that they append their start and finish times for the GET request with extra 0's - nine infact.

Use the same GET request format as mentioned in an answer above:

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

Now here is an example with the unix timestamp (php's 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 response I get:

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

However if you append your start and finish times with nine 0's that you put in your GET requests and shape your request like this:

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

It worked - this is the response I got:

{
  "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 response is a lot longer but I truncated it for the sake of this post. So when passing your datasets parameter into the request:

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

This did the trick for me, hopes it helps someone!

Countach
  • 597
  • 1
  • 10
  • 20
6

I tried post method with below URL & body. This will work, please check inline comments too.

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
}
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
  • Please can you format your code correctly using 4 spaces at the start of each line – Ethan Field Sep 01 '17 at 09:46
  • as Commented by @xanT, if we add 9 zeros, request will work as below. https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.step_count.delta:com.google.android.gms:estimated_steps/datasets/1504204200000000000-1504290000000000000 – Rajendra Singh Nagar Nov 05 '17 at 08:22