[Huawei Developers] Mike-mei:
HMS Core Health Kit provides REST APIs for apps to access its database and for them to provide app users with health and fitness services. As I wanted to implement health functions into my app, I chose to integrate Health Kit. While integrating the kit, I encountered and collected some common issues, as well as their solutions, related to this kit, which are all listed below. I hope you find this helpful.

Connectivity test fails after registering the subscription notification capability​

When you test the connectivity of the callback URL after registering as a subscriber, the system displays a message indicating that the connectivity test has failed and the returned status code is not 204.

Cause​

If the HTTP status code of the callback URL is not 204, 404 will be returned, indicating that the callback URL connectivity test has failed, even if you can access the URL.

Read Subscribing to Data for reference.

Solution​

Make sure that the URL is accessible and the returned status code is 204.

The total number of steps returned by the sampling data statistics API is inconsistent with the value calculated based on the step details​

Obtain the total number of steps by calling the API for Querying Sampling Data Statistics.

API URL: https://health-api.cloud.huawei.com/healthkit/v1/sampleSet:polymerize

Request parameters:

{
"polymerizeWith": [
{
"dataTypeName": "com.huawei.continuous.steps.delta"
}
],
"endTime": 1651809600000,
"startTime": 1651766400000,
"groupByTime": {
"groupPeriod": {
"timeZone": "+0800",
"unit": "day",
"value": 1
}
}
}

As shown below, the total number of steps returned is 7118.

View attachment 5783213

Obtain step details by calling the Querying Sampling Data Details API and calculate the total number of steps.

API URL: https://health-api.cloud.huawei.com/healthkit/v1/sampleSet:polymerize

Request parameters:

{
"polymerizeWith": [
{
"dataTypeName": "com.huawei.continuous.steps.delta"
}
],
"endTime": 1651809600000,
"startTime": 1651766400000
}

As shown below, the total number of steps calculated based on the returned result is 6280.

View attachment 5783215

As we can see, the total number of steps generated in a time segment returned by the sampling data statistics API differs from the value calculated based on the step details.

Cause​

As detailed data and statistical data are reported separately, detailed data delay or loss may lead to such inconsistencies.

When you query the data of a day as follows, you will obtain statistical data, rather than the value calculated based on the detailed data.

View attachment 5783223

Solution​

When querying the total number of steps, pass the groupByTime parameter, and set the duration parameter.

Request parameters:

{
"polymerizeWith": [
{
"dataTypeName": "com.huawei.continuous.steps.delta"
}
],
"endTime": 1651809600000,
"startTime": 1651766400000,
"groupByTime": {
"duration": 86400000
}
}

As shown below, the returned value is 6280, similar to what you calculated based on the detailed data.

View attachment 5783217

Error code 403 is returned, with the message "Insufficient Permission: Request had insufficient authentication scopes."​

View attachment 5783225

Cause​

Error 403 indicates that the request has been rejected. This error occurs when your app does not have sufficient scopes.

Solution​

1. Check whether you have applied for relevant scopes on the HUAWEI Developers Console.

View attachment 5783227

2. Check whether you have passed the scopes during authorization, and whether users have granted your app these scopes.

The following is an example of passing the step count read scope during authorization.

View attachment 5783231

Make sure that users have selected the relevant scopes when authorizing your app.

View attachment 5783237

Error code 400 is returned, with the message "Insufficient Permission: Request had insufficient authentication scopes."​

Let us take querying step count details as an example.

Let's say that the request parameters are set as follows:

Access token: generated based on the code of the first authorization.

Time of the first authorization (time when the code is generated for the first time): about 8:00 AM on May 7, 2022.

Time range of data collection:

Start time: 2022-05-06 00:00:00 (1651766400000)

End time: 2022-05-06 12:00:00 (1651809600000)

Request:

View attachment 5783241

Response:

View attachment 5783245

Cause​

To protect user data, you are only allowed to read data generated after a user has authorized you to do so. To read historical data generated before a user has granted authorization, you will need to obtain the read historical data scope. If the user does not grant your app this scope, and the start time you set for querying data is earlier than the time you obtained the user's authorization, the start time will revert to the time you first obtained the user's authorization. In this case, error 400 (invalid startTime or endTime) will be reported once the corrected start time is later than the end time you set, or only data generated after the authorization will be available.

In this example, the user does not grant the app the read historical data scope. The start date is May 6, whereas the date when the user authorized the app is May 7. In this case, the start date will be automatically adjusted to May 7, which is later than May 6, the end date. That is why error 400 (invalid startTime or endTime) is returned.

Suggestion​

1. Check whether you have applied for the read historical data scope on the HUAWEI Developers Console.

View attachment 5783247

Currently, historical data is available by week, month, or year. You can query historical data generated as early as one year before a user's authorization is acquired.

Scope	Description	Remarks

https://www.huawei.com/healthkit/historydata.open.week

Reads the previous week's data from Health Kit.	Only the previous week's data before the user authorization can be read.

https://www.huawei.com/healthkit/historydata.open.month

Reads the previous month's data from Health Kit.	Only the previous month's data before the user authorization can be read.

https://www.huawei.com/healthkit/historydata.open.year

Reads the previous year's data from Health Kit.	Only the previous year's data before the user authorization can be read.

2. When generating an authorization code, add the scopes listed in the preceding table, so that users can grant your app the read historical data scope after logging in to their HUAWEI ID.

Data queried after the authorization:

View attachment 5783249

References​

HMS Core Health Kit