0

I am trying to get my weekly aggregated step count from Google Fit using their REST Api following their documentation but the point data is coming back empty. I have enabled read location and activity scope for the access token. My code :

import json
import requests

api_url = "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"

access_token = ""

headers = {
  "Authorization": "Bearer {}".format(access_token),
  "Content-Type": "application/json;encoding=utf-8"
  }

body = {
  "aggregateBy": [{
    "dataTypeName": "com.google.step_count.delta",
    "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],
  "bucketByTime": { "durationMillis": 86400000 },
  "startTimeMillis": 1438705622000,
  "endTimeMillis": 1439310422000
}


response = requests.post(api_url, data=json.dumps(body), headers=headers)

print(response.text)
Oleg
  • 31
  • 6

1 Answers1

1
  "aggregateBy": [{
    "dataTypeName": "com.google.step_count.delta",
    "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
  }],

Your aggregateBy specifies both the data type name and data source id; the latter takes precedence, so this will only return data if the estimated_steps stream contains data on the server.

I'd suggest removing the dataSourceId, then the default data source for step_count.delta will be used.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • I am still getting an empty point dictionary after removing **dataSourceId** – Oleg Nov 13 '20 at 09:23
  • Do you have data in the data source if you just try to read it, without aggregating? – Andy Turner Nov 13 '20 at 09:34
  • Yes I do, I use Google Fit daily and I can be able to view my activity data from the app – Oleg Nov 13 '20 at 10:29
  • OK, but have you managed to issue a request to get that data without aggregation via the REST API? – Andy Turner Nov 13 '20 at 10:36
  • I cannot find a google fit's API endpoint to access the unaggregated data for my steps count. Would you please direct me? – Oleg Nov 13 '20 at 11:41
  • https://developers.google.com/fit/rest/v1/reference/users/dataSources/datasets/get - you can try it in the docs, and then paste the URL here so I can see what you've tried. – Andy Turner Nov 13 '20 at 13:39
  • ```https://www.googleapis.com/fitness/v1/users/me/dataSources/derived:com.google.activity.segment:com.google.android.gms:HMD Global:Nokia 3.2:b5dc902a:activity_from_steps/datasets/1438705622000-1439310422000 ``` This is the request I've used for dataSource request. The response I get from that url is ```{ "minStartTimeNs": "1438705622000", "maxEndTimeNs": "1439310422000", "dataSourceId": "derived:com.google.activity.segment:com.google.android.gms:HMD Global:Nokia 3.2:b5dc902a:activity_from_steps", "point": [] } ``` – Oleg Nov 13 '20 at 13:53
  • `1438705622000` - note that's in nanos - is Thu 01 Jan 1970 01:23:58 AM BST. Also, that request is for an activity segment data source, not the steps you're asking about. – Andy Turner Nov 13 '20 at 16:03
  • (Even in millis, the end time `1439310422000` is Tue 11 Aug 2015 05:27:02 PM BST. That's a while ago. Are you sure you expect data then?) – Andy Turner Nov 13 '20 at 16:16
  • Thank You so much I changed the millisecond's body and was able to get the step count data I need. My problem was I used the time provided in Google Fit's documentation. – Oleg Nov 14 '20 at 07:56