Get specific value out of Google Fit API in REST

Viewed 130

I am calling the google fit API for the activity between two dates but how do I just get the Activity integers? Is there a direct way to call it from the HTTP request or do I have to filter the JSON data myself?

    social_token = SocialToken.objects.get(account__user=2)
    token=social_token.token
    url = "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"

    headers = {
        "Authorization": "Bearer {}".format(token),
        "Content-Type": "application/json"
        }
    body = {
        "aggregateBy": [{
            "dataTypeName": "com.google.activity.segment",
      
        }],

        "startTimeMillis": 1634767200000,
        "endTimeMillis": 1634853600000
        }
    respo = requests.post(url, data=json.dumps(body), headers=headers)

The JSON response

[
    "{\n  \"bucket\": 
    [\n    {\n      \"startTimeMillis\": \"1634767200000\",\n      \"endTimeMillis\": \"1634853600000\",\n      \"dataset\": 
    [\n     ",
    "   {\n          \"dataSourceId\": \"derived:com.google.activity.segment:com.google.android.gms:merge_activity_segments\",\n          \"",
    "point\": 
    [\n            {\n              \"startTimeNanos\": \"1634818320000000000\",\n              \"endTimeNanos\": \"163482012000000000",
    "0\",\n              \"dataTypeName\": \"com.google.activity.segment\",\n              \"originDataSourceId\": \"raw:com.google.activity.se",
    "gment:com.google.android.apps.fitness:user_input\",\n              \"value\": [\n                {\n                  \"intVal\": 97,\n  ",
    "                \"mapVal\": []\n                }\n              ]\n            },\n            {\n              \"startTimeNanos\": \"163",
    "4820120000000000\",\n              \"endTimeNanos\": \"1634820292573000000\",\n              \"dataTypeName\": \"com.google.activity.segme",
    "nt\",\n              \"value\": [\n                {\n                  \"intVal\": 7,\n                  \"mapVal\": []\n                }\n",
    "              ]\n            },\n            {\n              \"startTimeNanos\": \"1634823157245000000\",\n              \"endTimeNanos\"",
    ": \"1634823301721000000\",\n              \"dataTypeName\": \"com.google.activity.segment\",\n              \"value\": [\n                {",
    "\n                  \"intVal\": 7,\n                  \"mapVal\": []\n                }\n              ]\n            }\n          ]\n     ",
    "   }\n      ]\n    }\n  ]\n}\n"
]
1 Answers

To get all individual entries for a datatype between a range, you need to use the Users.dataSources.datasets: get API.

The request method will be GET and the endpoint will be

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

where datasetId is of the format StartTimeInNanoseconds-EndTimeInNanoseconds and dataSourceId will be the ID of the dataource against which you want the individual entries. For example, for body temperature, it will be derived:com.google.body.temperature:com.google.android.gms:merged.

To get the list of all the available data source IDs your application has access to, call the API

GET https://www.googleapis.com/fitness/v1/users/me/dataSources

Sample request to get body temperature

GET https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.body.temperature:com.google.android.gms:merged/datasets/1655750425675000000-1655991535675000000

Response

{
  "minStartTimeNs": "1655750425675000000", 
  "maxEndTimeNs": "1655991535675000000", 
  "dataSourceId": "derived:com.google.body.temperature:com.google.android.gms:merged", 
  "point": [
    {
      "modifiedTimeMillis": "1655961443186", 
      "startTimeNanos": "1655944200000000000", 
      "endTimeNanos": "1655944200000000000", 
      "value": [
        {
          "mapVal": [], 
          "fpVal": 30
        }, 
        {
          "mapVal": []
        }
      ], 
      "dataTypeName": "com.google.body.temperature", 
      "originDataSourceId": "raw:com.google.body.temperature:com.google.android.apps.fitness:user_input"
    }, 
    {
      "modifiedTimeMillis": "1655961443186", 
      "startTimeNanos": "1655947800000000000", 
      "endTimeNanos": "1655947800000000000", 
      "value": [
        {
          "mapVal": [], 
          "fpVal": 34
        }, 
        {
          "mapVal": []
        }
      ], 
      "dataTypeName": "com.google.body.temperature", 
      "originDataSourceId": "raw:com.google.body.temperature:com.google.android.apps.fitness:user_input"
    }, 
    {
      "modifiedTimeMillis": "1655961443186", 
      "startTimeNanos": "1655955000000000000", 
      "endTimeNanos": "1655955000000000000", 
      "value": [
        {
          "mapVal": [], 
          "fpVal": 38
        }, 
        {
          "mapVal": []
        }
      ], 
      "dataTypeName": "com.google.body.temperature", 
      "originDataSourceId": "raw:com.google.body.temperature:com.google.android.apps.fitness:user_input"
    }
  ]
}
Related