Max
Max

Reputation: 674

Getting activities list with a distances in Google Fit API (REST)

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?

Upvotes: 1

Views: 1142

Answers (1)

Max
Max

Reputation: 674

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": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

Upvotes: 1

Related Questions