Health Service kit 读取用户的锻炼记录 报错code码201
AsyncInvokeStub throw error: {“code”:201,“message”:“Permission verification failed, The app does not have WORKOUT detail read permission.”,“name”:“HealthKitError”}
授权也授权了读取walking 的权限
操作步骤:
1、
export class AuthManagement {
public static async auth(context: common.UIAbilityContext): Promise<boolean> {
try {
let authorizationParameter: healthStore.AuthorizationRequest = {
readDataTypes: [healthStore.exerciseSequenceHelper.DATA_TYPE],
writeDataTypes: [healthStore.exerciseSequenceHelper.DATA_TYPE]
}
let res: healthStore.AuthorizationResponse =
await healthStore.requestAuthorizations(context, authorizationParameter);
let grantedWriteDataTypes: healthStore.DataType[] = res.writeDataTypes;
let grantedReadDataTypes: healthStore.DataType[] = res.readDataTypes;
Logger.error(`Health=auth=try==>${JSON.stringify(res)}`)
return true;
} catch (err) {
Logger.error(`Health=auth=catch==>${JSON.stringify(err)}`)
return false;
}
}
}
2、
export class HealthManager {
public static async readData(startTimeParam: number, endTimeParam: number): Promise<string> {
try {
const startTime = startTimeParam; // 2023-10-23 14:00:00
const endTime = endTimeParam; // 2023-10-23 14:30:00
// 2. 查询跑步记录
const sequenceReadRequest: healthStore.ExerciseSequenceReadRequest = {
startTime: startTime,
endTime: endTime,
exerciseType: healthStore.exerciseSequenceHelper.walking.EXERCISE_TYPE,
count: 1,
sortOrder: 1,
readOptions: {
withDetails: true,
}
};
healthStore.readData<healthStore.exerciseSequenceHelper.walking.Model>(sequenceReadRequest).then((value) => {
Logger.debug(`health=============>>>>>>>${value}`)
});
return 'readData success ';
} catch (err) {
return 'readData fail :\n' + JSON.stringify(err);
}
}
}
然后就出现AsyncInvokeStub throw error: {“code”:201,“message”:“Permission verification failed, The app does not have WORKOUT detail read permission.”,“name”:“HealthKitError”}
从报错信息上分析,是应用权限的问题,请参考如下文档确认权限设置是正确的:
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/health-service-kit-guide-V5