1e41f4b71Sopenharmony_ci# Requesting User Authorization
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciBefore an application accesses user privacy information or use a system capability, for example, to obtain location information, access the Calendar, or use the camera to take a photo or record a video, the application needs to request user authorization. The permissions that must be authorized by users are user_grant permissions.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe procedure for requesting user_grant permissions is as follows:
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci1. Declare the permissions required by your application in the configuration file. 
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci2. Associate each object that requires a user_grant permission with the related permission. This lets the user know what operations need user authorization.<br>
10e41f4b71Sopenharmony_ci   For details about the preceding two steps, see [Declaring Permissions](declare-permissions.md).
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci3. Trigger user authorization via an API when the application in running needs to access the target object. The API first checks whether the user has granted the permission required. If no, a dialog box will be displayed to request authorization from the user.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci4. Check the user authorization result, and allow the next step only after the user has granted the permission to the application.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciThis topic elaborates steps 3 and 4.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci## Constraints
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci- A check for the required permission is mandatory each time before the operation that requires the permission is performed.
21e41f4b71Sopenharmony_ci  
22e41f4b71Sopenharmony_ci  You can use [checkAccessToken()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#checkaccesstoken9) to check whether the user has granted specific permissions to your application. This API returns [PERMISSION_GRANTED](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#grantstatus) or [PERMISSION_DENIED](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#grantstatus). For details, see the example given below.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- Each time before an API that requires a **user_grant** permission is called, use [requestPermissionsFromUser()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9) to check whether the user has already granted the permission.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci  After a permission is granted, the user may revoke the permission in **Settings**. Therefore, the previous authorization status cannot be persistent.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- For a user_grant permission, show a rationale to the user in a UI element, clearly explaining why your application needs the permission. Based on the rationale, the user then determines whether to grant the permission.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci- Frequent pop-up windows may disturb user experience and are not recommended. If a user rejects the authorization, the window for requesting user authorization will not be displayed again. The application needs to provide information to guide the user to manually grant the permission in **Settings**.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci- The system permission pop-up window cannot be obscured.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci  The system permission pop-up window cannot be obscured by other components. The information in the pop-up window must be completely displayed so that the user can identify and complete authorization.
35e41f4b71Sopenharmony_ci  If the system permission pop-up window is displayed in the same position as another component, the system permission pop-up window takes precedence over the other component by default.
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci## How to Develop
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciThe following example steps you through on how to request the permission for using the microphone.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**Figure** Requesting user authorization
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci![](figures/request_user_authorization.png)
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci1. Declare the ohos.permission.MICROPHONE permission in the configuration file. For details, see [Declaring Permissions](declare-permissions.md).
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci2. Check whether the user has granted the permission.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci   Use [checkAccessToken()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#checkaccesstoken9) to check whether the user has already granted the permission that your application requires. If yes, the application can use the microphone. Otherwise, user authorization is required.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci   ```ts
52e41f4b71Sopenharmony_ci   import { abilityAccessCtrl, bundleManager, Permissions } from '@kit.AbilityKit';
53e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
54e41f4b71Sopenharmony_ci   
55e41f4b71Sopenharmony_ci   const permissions: Array<Permissions> = ['ohos.permission.MICROPHONE'];
56e41f4b71Sopenharmony_ci   
57e41f4b71Sopenharmony_ci   async function checkPermissionGrant(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> {
58e41f4b71Sopenharmony_ci     let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
59e41f4b71Sopenharmony_ci     let grantStatus: abilityAccessCtrl.GrantStatus = abilityAccessCtrl.GrantStatus.PERMISSION_DENIED;
60e41f4b71Sopenharmony_ci   
61e41f4b71Sopenharmony_ci     // Obtain the token ID of the application.
62e41f4b71Sopenharmony_ci     let tokenId: number = 0;
63e41f4b71Sopenharmony_ci     try {
64e41f4b71Sopenharmony_ci       let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
65e41f4b71Sopenharmony_ci       let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo;
66e41f4b71Sopenharmony_ci       tokenId = appInfo.accessTokenId;
67e41f4b71Sopenharmony_ci     } catch (error) {
68e41f4b71Sopenharmony_ci       const err: BusinessError = error as BusinessError;
69e41f4b71Sopenharmony_ci       console.error(`Failed to get bundle info for self. Code is ${err.code}, message is ${err.message}`);
70e41f4b71Sopenharmony_ci     }
71e41f4b71Sopenharmony_ci   
72e41f4b71Sopenharmony_ci     // Check whether the user has granted the permission.
73e41f4b71Sopenharmony_ci     try {
74e41f4b71Sopenharmony_ci       grantStatus = await atManager.checkAccessToken(tokenId, permission);
75e41f4b71Sopenharmony_ci     } catch (error) {
76e41f4b71Sopenharmony_ci       const err: BusinessError = error as BusinessError;
77e41f4b71Sopenharmony_ci       console.error(`Failed to check access token. Code is ${err.code}, message is ${err.message}`);
78e41f4b71Sopenharmony_ci     }
79e41f4b71Sopenharmony_ci   
80e41f4b71Sopenharmony_ci     return grantStatus;
81e41f4b71Sopenharmony_ci   }
82e41f4b71Sopenharmony_ci   
83e41f4b71Sopenharmony_ci   async function checkPermissions(): Promise<void> {
84e41f4b71Sopenharmony_ci     let grantStatus: abilityAccessCtrl.GrantStatus = await checkPermissionGrant(permissions[0]);
85e41f4b71Sopenharmony_ci   
86e41f4b71Sopenharmony_ci     if (grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
87e41f4b71Sopenharmony_ci       // If the user has granted the permission, the application can use the microphone.
88e41f4b71Sopenharmony_ci     } else {
89e41f4b71Sopenharmony_ci       // Request the permission for using the microphone.
90e41f4b71Sopenharmony_ci     }
91e41f4b71Sopenharmony_ci   }
92e41f4b71Sopenharmony_ci   ```
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci3. Request user authorization when your application needs to access the microphone.
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci   Use [requestPermissionsFromUser()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9) to request user authorization. You can specify a list of permissions, such as the permission to access the location, Calendar, camera, or microphone, in the **Array\<Permissions>** parameter of this API. The user can grant or deny the permissions.
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci   You can have [requestPermissionsFromUser()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9) called in **onWindowStageCreate()** of the UIAbility to dynamically request user authorization, or request user authorization on the UI based on service requirements.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci   When applying for permissions using **onWindowStageCreate()**, the application needs to wait until the **loadContent()** or **setUIContent()** API is complete or call **[requestPermissionsFromUser()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9)** in **loadContent()** or **setUIContent()**. Otherwise, **requestPermissionsFromUser()** will fail before **Content** is loaded.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci   <!--RP1--><!--RP1End-->
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci   <!--RP2-->
105e41f4b71Sopenharmony_ci   - Sample code for requesting user authorization using UIAbility
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci      ```ts
108e41f4b71Sopenharmony_ci      import { abilityAccessCtrl, common, Permissions, UIAbility } from '@kit.AbilityKit';
109e41f4b71Sopenharmony_ci      import { window } from '@kit.ArkUI';
110e41f4b71Sopenharmony_ci      import { BusinessError } from '@kit.BasicServicesKit';
111e41f4b71Sopenharmony_ci      
112e41f4b71Sopenharmony_ci      const permissions: Array<Permissions> = ['ohos.permission.MICROPHONE'];
113e41f4b71Sopenharmony_ci      function reqPermissionsFromUser(permissions: Array<Permissions>, context: common.UIAbilityContext): void {
114e41f4b71Sopenharmony_ci        let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
115e41f4b71Sopenharmony_ci        // Determine whether to display a user authorization dialog box based on the return value of requestPermissionsFromUser.
116e41f4b71Sopenharmony_ci        atManager.requestPermissionsFromUser(context, permissions).then((data) => {
117e41f4b71Sopenharmony_ci          let grantStatus: Array<number> = data.authResults;
118e41f4b71Sopenharmony_ci          let length: number = grantStatus.length;
119e41f4b71Sopenharmony_ci          for (let i = 0; i < length; i++) {
120e41f4b71Sopenharmony_ci            if (grantStatus[i] === 0) {
121e41f4b71Sopenharmony_ci              // If the user grants the permission, the application can use the microphone.
122e41f4b71Sopenharmony_ci            } else {
123e41f4b71Sopenharmony_ci              // If the user denies the permission, display a message indicating that user authorization is required, and direct the user to set the permission in the Settings page.
124e41f4b71Sopenharmony_ci              return;
125e41f4b71Sopenharmony_ci            }
126e41f4b71Sopenharmony_ci          }
127e41f4b71Sopenharmony_ci          // The authorization is successful.
128e41f4b71Sopenharmony_ci        }).catch((err: BusinessError) => {
129e41f4b71Sopenharmony_ci          console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
130e41f4b71Sopenharmony_ci        })
131e41f4b71Sopenharmony_ci      }
132e41f4b71Sopenharmony_ci      export default class EntryAbility extends UIAbility {
133e41f4b71Sopenharmony_ci        onWindowStageCreate(windowStage: window.WindowStage): void {
134e41f4b71Sopenharmony_ci          // ...
135e41f4b71Sopenharmony_ci          windowStage.loadContent('pages/Index', (err, data) => {
136e41f4b71Sopenharmony_ci            reqPermissionsFromUser(permissions, this.context);
137e41f4b71Sopenharmony_ci          // ...
138e41f4b71Sopenharmony_ci          });
139e41f4b71Sopenharmony_ci        }
140e41f4b71Sopenharmony_ci      
141e41f4b71Sopenharmony_ci        // ...
142e41f4b71Sopenharmony_ci      }
143e41f4b71Sopenharmony_ci      ```
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci   - Sample code for requesting user authorization on the UI
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci      ```ts
148e41f4b71Sopenharmony_ci      import { abilityAccessCtrl, common, Permissions } from '@kit.AbilityKit';
149e41f4b71Sopenharmony_ci      import { BusinessError } from '@kit.BasicServicesKit';
150e41f4b71Sopenharmony_ci      
151e41f4b71Sopenharmony_ci      const permissions: Array<Permissions> = ['ohos.permission.MICROPHONE'];
152e41f4b71Sopenharmony_ci      function reqPermissionsFromUser(permissions: Array<Permissions>, context: common.UIAbilityContext): void {
153e41f4b71Sopenharmony_ci        let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager();
154e41f4b71Sopenharmony_ci        // Determine whether to display a user authorization dialog box based on the return value of requestPermissionsFromUser.
155e41f4b71Sopenharmony_ci        atManager.requestPermissionsFromUser(context, permissions).then((data) => {
156e41f4b71Sopenharmony_ci          let grantStatus: Array<number> = data.authResults;
157e41f4b71Sopenharmony_ci          let length: number = grantStatus.length;
158e41f4b71Sopenharmony_ci          for (let i = 0; i < length; i++) {
159e41f4b71Sopenharmony_ci            if (grantStatus[i] === 0) {
160e41f4b71Sopenharmony_ci              // If the user grants the permission, the application can use the microphone.
161e41f4b71Sopenharmony_ci            } else {
162e41f4b71Sopenharmony_ci              // If the user denies the permission, display a message indicating that user authorization is required, and direct the user to set the permission in the Settings page.
163e41f4b71Sopenharmony_ci              return;
164e41f4b71Sopenharmony_ci            }
165e41f4b71Sopenharmony_ci          }
166e41f4b71Sopenharmony_ci          // The authorization is successful.
167e41f4b71Sopenharmony_ci        }).catch((err: BusinessError) => {
168e41f4b71Sopenharmony_ci          console.error(`Failed to request permissions from user. Code is ${err.code}, message is ${err.message}`);
169e41f4b71Sopenharmony_ci        })
170e41f4b71Sopenharmony_ci      }
171e41f4b71Sopenharmony_ci      
172e41f4b71Sopenharmony_ci      @Entry
173e41f4b71Sopenharmony_ci      @Component
174e41f4b71Sopenharmony_ci      struct Index {
175e41f4b71Sopenharmony_ci        aboutToAppear() {
176e41f4b71Sopenharmony_ci          const context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
177e41f4b71Sopenharmony_ci          reqPermissionsFromUser(permissions, context);
178e41f4b71Sopenharmony_ci        }
179e41f4b71Sopenharmony_ci      
180e41f4b71Sopenharmony_ci        build() {
181e41f4b71Sopenharmony_ci          // ...
182e41f4b71Sopenharmony_ci        }
183e41f4b71Sopenharmony_ci      }
184e41f4b71Sopenharmony_ci      ```
185e41f4b71Sopenharmony_ci   <!--RP2End-->
186e41f4b71Sopenharmony_ci
187e41f4b71Sopenharmony_ci4. Perform subsequent operations based on the authorization result.
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci   After [requestPermissionsFromUser()](../../reference/apis-ability-kit/js-apis-abilityAccessCtrl.md#requestpermissionsfromuser9) is called, the application waits for the user authorization result. If the user has granted the permission, the application can use the microphone. Otherwise, display a message indicating that user authorization is required, and direct the user to set the permission in the **Settings** page.<!--RP3-->
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci   Path: **Settings**\> **Privacy**\> **Permission manager**\> **Apps**\> Target app<!--RP3End-->
192