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

╄→尐↘猪︶ㄣ 提交于 2020-01-10 02:48:12

问题


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?


回答1:


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.




回答2:


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!




回答3:


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
}


来源:https://stackoverflow.com/questions/27158645/how-do-i-retrieve-step-count-data-from-google-fitness-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!