获取距离 Google 适合 API 的活动列表(REST)

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

我需要在 Google Fit 中获取最近的活动列表,包括行进的距离。

我正在使用直接 REST API 请求(PHP 通过 cURL)。

  1. 首先,我通过 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. 授权后,我被重定向到我的站点%REDIRECT_URI%,其中 GET 请求包含“代码”。

  2. 我使用此“代码”获取授权令牌:

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. 完成请求后,我收到“ya29.a0ARrdaM-...”形式的身份验证令牌,我将此令牌用于header 承载授权。

  2. 我请求https://www.googleapis.com/fitness/v1/users/me/sessions获取活动列表:

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

根据这个列表,我可以看到 运行 时间(startTimeMillis,endTimeMillis),但我仍然需要获取距离。

接下来我该做什么?

根据问题采取的所有行动都是正确的。

接下来,您需要向https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate

发送请求

CURL 请求:

您可以删除“<---”注释并将此 CURL 代码推送给 Postman 以使用 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
  }'

响应:

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