Search code examples
google-fit

How to read Blood glucose data from Google Fit?


I am trying to get blood glucose from google fit account, if user logged or synced any blood glucose data in their account.

API,

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

Body

    {
      "aggregateBy": [{
           "dataTypeName": "com.google.blood_glucose",
           "dataSourceId": "derived:com.google.blood_glucose:com.google.android.gms:merged"
                      }],
      "bucketByTime": { "durationMillis": 86400000 },
      "startTimeMillis": 1578718800000, // jan 11 2010
      "endTimeMillis": 1580360400000 // jan 30 2020
}

I am receiving :

{
    "error": {
        "errors": [
            {
                "domain": "global",
                "reason": "forbidden",
                "message": "datasource not found or not readable: derived:com.google.blood_glucose:com.google.android.gms:delta"
            }
        ],
        "code": 403,
        "message": "datasource not found or not readable: derived:com.google.blood_glucose:com.google.android.gms:delta"
    }
}

Any suggestions on how to read blood glucose data if user have synced or logged anything in their google fit account ?


Solution

  • You don't need to specify both the data type name and data source ID. In fact, if you specify both, the data type name is (currently) ignored.

    If you specify the data source ID, that data source has to exist; if you specify the data type name, the "default" data source for that type is used; but it is allowed not to exist (for example, if the user has never actually written blood glucose data).

    So, change your request to:

    {
      "aggregateBy": [{
           "dataTypeName": "com.google.blood_glucose",
                      }],
      "bucketByTime": { "durationMillis": 86400000 },
      "startTimeMillis": 1578718800000,
      "endTimeMillis": 1580360400000
    }
    

    Try it now