User authorization is required before certain APIs are used. These APIs are divided into multiple scopes based on their use cases. When a user grants authorization for a scope, all APIs under that scope can be called.
The permissions required for calling the APIs must be declared one by one in the project configuration file. Otherwise, the atomic service cannot obtain the authorization, resulting in the failure of API calls. For details about the permission declaration fields in the configuration file, please refer to Declaring Permissions in the Configuration File.
Before the related service functionality is executed, initiate a request for user authorization. After the user explicitly grants or rejects authorization, the authorization result is recorded in the backend and persists until the user deletes the atomic service.
The following situations may occur when APIs requiring user authorization are called:
If the user has never granted or rejected the authorization request for this scope, no dialog will be displayed.
If the user has granted authorization, the API can be called directly.
If the user has rejected authorization, no dialog will be displayed, and the fail callback of the API will be triggered. You need to ensure compatibility with the scenario where users reject authorization.
The authorization dialog displays the permission usage declared in the user privacy protection guide of the atomic service. The permission usage description must comply with Specifications for reason.
Users can choose Settings > Manage service of an atomic service to set the permissions for the atomic service.
Call has.openSetting to open the Settings page and guide the user to set permissions.
has.openSetting({
success: (res) => {
console.info('openSetting success', res);
},
fail: (err) => {
console.error('openSetting fail', err);
},
complete: (res) => {
console.info('openSetting complete', res);
}
}); Call has.authorize to initiate a request for user authorization before the related API is called.
has.authorize({
scope: 'scope.userLocation',
success: (res) => {
console.info('authorize success', res);
},
fail: (err) => {
console.error('authorize fail', err);
},
complete: (res) => {
console.info('authorize complete', res);
}
}); Before initiating a request for user authorization, you must declare the permissions required in the app configuration file. For details, please refer to Declaring Permissions.
| Scope | API | Description | Required Permission |
|---|---|---|---|
| scope.userLocation | Precise geographical location | ohos.permission.LOCATION ohos.permission.APPROXIMATELY_LOCATION | |
| scope.userFuzzyLocation | Approximate geographical location | ohos.permission.APPROXIMATELY_LOCATION | |
| scope.camera | camera component | Camera | ohos.permission.CAMERA |
| scope.addPhoneCalendar | Adding events to the calendar | ohos.permission.READ_CALENDAR ohos.permission.WRITE_CALENDAR | |
| scope.bluetooth | All APIs in Bluetooth - General All APIs in Bluetooth - BLE | Bluetooth | ohos.permission.ACCESS_BLUETOOTH |
| scope.record | camera component | Microphone | ohos.permission.MICROPHONE |