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

后端 未结 3 694
遇见更好的自我
遇见更好的自我 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:37

    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
    }
    
    0 讨论(0)
  • 2020-12-16 02:41

    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.

    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题