How do I retrieve step count data from Google Fitness REST api?

后端 未结 3 698
遇见更好的自我
遇见更好的自我 2020-12-16 01:55

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

3条回答
  •  醉酒成梦
    2020-12-16 02:42

    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!

提交回复
热议问题