This topic describes how to integrate mobile number verification with the HarmonyOS client and provides examples of how to use the SDK APIs.
The one-click logon service works only when cellular data is enabled and the application has permission to use the cellular network.
The number retrieval request consumes a small amount of data.
China Mobile supports 2G, 3G, 4G, and 5G data networks. On 2G and 3G networks, latency is higher and the success rate is lower than on 4G networks.
China Telecom supports 4G and 5G data networks, but not 2G or 3G.
China Unicom supports 3G, 4G, and 5G data networks, but not 2G.
Procedure
Request a certification scheme key
API calls require a scheme code and a key. To obtain them, go to the Phone Number Verification Service console and create a certification scheme. You will need to provide your app name, package name, package signature, and AppId.
Get the signature
Use the following HarmonyOS code to get the package name, package signature, and AppId.
bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO).then((bundleInfo) => {
const packageName = bundleInfo.name
console.log("numberauth:pagname:" + packageName)
const sign = bundleInfo.signatureInfo.fingerprint
console.log("numberauth:sign:" + sign)
const appIdentifier = bundleInfo.signatureInfo.appIdentifier
console.log("numberauth:appid:" + appIdentifier)
})Sign your app
If you have not yet signed your new project, use the automatic signing feature in DevEco Studio:
-
To use automatic signing, click the Project Structure icon in the upper-right corner of the IDE.
This square-shaped icon is in the toolbar, to the right of the green Run button.
-
In the dialog box, go to Project > Signing Configs, select Automatically generate signature, wait for the signing to complete, and then click OK.
For more information about configuring app signatures for HarmonyOS apps, see the official HarmonyOS documentation.
Get the SDK
Log in to the Phone Number Verification Service console. On the overview page, in the API & SDK section on the right, click Download Now. On the API & SDK page, select Phone Number Verification Service for HarmonyOS.
Import the project
Create or open your project in DevEco Studio.
Extract the downloaded package and copy the
.harfile to thelibsdirectory in your project.
Add the SDK dependency to the
oh_package.json5file in your module:Note: This is the
oh_package.json5file in the module, not the one in the project's root directory. For example, refer to the demo project in the package."dependencies": { "numberauth_standard": "file:libs/auth_number_product-2.0.1-log-online-standard-release.har" }
Configure permissions
Edit the module.json5 file to configure the required permissions for the SDK:
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
},
{
"name": "ohos.permission.GET_NETWORK_INFO"
},
{
"name": "ohos.permission.SET_NETWORK_INFO"
}
]Permission | Description |
INTERNET | Lets your application connect to the internet to access the gateway and authentication server. |
GET_NETWORK_INFO | Lets your application get the network status to determine the connection type, such as cellular data or Wi-Fi. |
SET_NETWORK_INFO | Lets your application configure the cellular data network. This permission is required to switch to the cellular network for number retrieval. |
Interaction flow
For the complete interaction flow, see mobile number verification.
SDK API reference
Required: Create an entry point instance
PhoneNumberAuthHelper is the entry point of the SDK. All API calls must be made through PhoneNumberAuthHelper.
Required: Set the secret key
public setAuthSDKInfo(secretInfo: string): voidParameters:
Parameter | Type | Description |
secretInfo | string | The SDK secret key. You can obtain it by creating an authentication scheme in the Phone Number Verification Service console. |
Required: Clear the flow callback listener
After the flow completes, call this method to clear the listener and prevent unnecessary background processing.
public clearAuthListener():voidRequired method: Get the mobile number verification token
Call this method to get the mobile number verification token.
// totalTime: The timeout in milliseconds (ms).
public getVerifyToken(totalTime: number): Promise<void>;;
Parameters:
|
Parameter |
Type |
Description |
|
totalTime |
number |
The timeout, in milliseconds. |
Example:
this.helper.getVerifyToken(5000)
The token is returned to the listener's callback:
class TokenListener implements TokenResultListener{
private page:Index
constructor(page: Index) {
this.page = page;
}
onSuccess(msg: string): void {
console.log("auth:onSuccess:"+msg)
const result:object=JSON.parse(msg)
const code=result['_code']+''
const token=result['_token']+''
if(code=="600000"){
this.page.quitLoginPage()
}
}
onFailure(ret: string): void {
console.log("auth:onFailure:"+ret)
this.page.updateAuth()
}
}
Optional method: Accelerate mobile number verification
This method can accelerate token acquisition for mobile number verification. You can call this method in advance, for example, during the initialization of your HAP.
public accelerateVerify(overdueTimeMills: number, listener: PreLoginResultListener): void
Parameters:
|
Parameter |
Type |
Description |
|
overdueTimeMills |
number |
The timeout, in milliseconds (ms). |
|
listener |
PreLoginResultListener |
The pre-login callback listener. |
Optional: Set the callback listener
Adds or updates the listener for the authentication flow.
public setAuthListener(listener: TokenResultListener): voidParameters:
Parameter | Type | Description |
listener | TokenResultListener | The listener to add or update. |
Optional: Check the runtime environment
This method checks if the device's runtime environment is suitable for the SDK and returns the result in the callback.
public checkEnvAvailable(type: number): voidParameters:
Parameter | Type | Description |
type | number | The SDK feature type. Valid values:
|
SDK return codes
For the return codes from the Phone Number Verification HarmonyOS client SDK, see Alibaba Cloud Phone Number Verification SDK return code reference (HarmonyOS client only).
Troubleshoot SDK import failures
After adding the SDK dependency, click Sync in the upper-right corner of the IDE to import it.
If the import fails, follow these steps:
-
Delete the
oh_modulesdirectory from the module and the project's root directory. -
Then, run
ohpm installin the Terminal at the bottom of DevEco Studio to re-import.ohpm install (node:11186) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) ohm INFO: remove useless folder succeed: "/Users/cmw01044812/Desktop/HmProject/NumberAuthDemo/oh_modules/.tmp" install completed in 0s 300ms ohpm install (node:11667) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) ohm INFO: remove useless folder succeed: "/Users/cmw01044812/Desktop/HmProject/NumberAuthDemo/oh_modules/.tmp" install completed in 0s 297ms ohpm install (node:12153) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) install completed in 0s 217ms