1

I need to get a list of recent activities in Google Fit, including the distance traveled.

I am using direct REST API requests (PHP via cURL).

  1. First, I do authorization via oAuth 2.0

https://accounts.google.com/o/oauth2/v2/auth?client_id=%CLIENT_ID%&redirect_uri=%REDIRECT_URI%&response_type=code&scope=https://www.googleapis.com/auth/fitness.activity.read%20https://www.googleapis.com/auth/fitness.location.read

  1. After authorization, I'm redirected to my site %REDIRECT_URI%, where the GET request contains "code".

  2. I use this "code" to get the auth token:

POST - https://www.googleapis.com/oauth2/v3/token
POST data: {
    'code': %CODE_FROM_GET_PARAM%,
    'client_id' => %CLIENT_ID%,
    'client_secret' => %CLIENT_SECRET%,
    'grant_type' => 'authorization_code',
    'redirect_uri' => %REDIRECT_URI%
}
  1. After completing the request, I receive an auth token of the form "ya29.a0ARrdaM-...", I use this token for header Bearer authorization.

  2. I make a request https://www.googleapis.com/fitness/v1/users/me/sessions to get a list of activities:

"session": [
        {
            "id": "Run1629350880000",
            "name": "Run",
            "description": "",
            "startTimeMillis": "1629350880000",
            "endTimeMillis": "1629352020000",
            "modifiedTimeMillis": "1629358291250",
            "application": {
                "packageName": "com.xiaomi.hm.health"
            },
            "activityType": 8
        }, ...
]

Based on this list, I can see the running time (startTimeMillis, endTimeMillis), but I still need to get the distance.

What should I do next?

Max
  • 664
  • 1
  • 6
  • 19
  • Please see this answer https://stackoverflow.com/questions/39783825/google-fit-api-get-distance-from-google-fit – mikegross Aug 19 '21 at 08:28
  • @mikegross This solution is for Android and it's not suitable for REST API – Max Aug 19 '21 at 08:37
  • Ouh my bad! after reading more carefully https://developers.google.com/fit/rest/v1/reference I see that there is no particular focus on distance. It might be that you have to record it yourself when the user is running. But I get why it is not recorded, fit API is to record many different sports (curling etc..) and not all of them have a distance associated to them. So probably Google Fit API does not store distances. – mikegross Aug 19 '21 at 08:44
  • The fields of Session are described [here](https://developers.google.com/fit/rest/v1/reference/users/sessions/list#response). Distance isn't a field recorded in Session. – Andy Turner Aug 19 '21 at 10:25

1 Answers1

1

All actions taken from the question are correct.

Next, you need to send a request to https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

CURL request:

You can remove "<---" comments and push this CURL code to Postman to see this request with UI

curl --location --request POST 'https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate' \
--header 'Authorization: Bearer %AUTH_TOKEN%' \ <--- Real Auth token
--header 'Content-Type: application/json' \
--data-raw '{
    "aggregateBy": [
      {
        "dataTypeName": "com.google.distance.delta",
        "dataSourceId": "derived:com.google.distance.delta:com.google.android.gms:merge_distance_delta"
      }
    ],
    "startTimeMillis": 1629350880000, <--- "startTimeMillis" from session data
    "endTimeMillis": 1629352020000    <--- "endTimeMillis" from session data
  }'

Response:

{
    "bucket": [
        {
            "startTimeMillis": "1629350880000",
            "endTimeMillis": "1629352020000",
            "dataset": [
                {
                    "dataSourceId": "derived:com.google.distance.delta:com.google.android.gms:merge_distance_delta",
                    "point": [
                        {
                            "startTimeNanos": "1629350880000000000",
                            "endTimeNanos": "1629352020000000000",
                            "dataTypeName": "com.google.distance.delta",
                            "originDataSourceId": "raw:com.google.distance.delta:com.xiaomi.hm.health:GoogleFitSyncHelper- distance0",
                            "value": [
                                {
                                    "fpVal": 874, <--- distance in meters
                                    "mapVal": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}
Max
  • 664
  • 1
  • 6
  • 19