[Huawei Developers] John Wo:
In this article, we’ll be looking at some of the APIs that Account Kit provides and learn how to use them in our Flutter projects.

Huawei Account Kit

Account Kit provides a simple and quick authorization experience to users. It enables to save time from long authorization periods and it’s two factor authentication process keeps users information safe. Account Kit allows you to connect to the Huawei ecosystem using your Huawei Id from a range of devices, such as mobile phones and tablets. Users can quickly and conveniently sign in to apps with their Huawei Ids after granting an initial access permission.

Configuring the project

Registering as a Developer

As first, you need to create a Huawei developer account and complete the identity verification. For details, please refer to Registering a Huawei Id.

Creating an APP

Sign in AppGallery Connect and create a new project.

Add a new app to your project by clicking the Add App button.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094722.99062874268286391826736169298462:50511019062808:2800:6942CB0FA8593B1AA4FD3CA491C56B3AD392EB2AD9C027C078E495BC2D6BA439.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094722.99062874268286391826736169298462:50511019062808:2800:6942CB0FA8593B1AA4FD3CA491C56B3AD392EB2AD9C027C078E495BC2D6BA439.png[/IMG]

Generating a Signing Certificate Fingerprinter

Signing certificate fingerprints are used to verify the authenticity of an App when it attempts to access an HMS Core service through the HMS Core SDK. So before using the HMS Core service, you must generate a signing certificate fingerprint and configure it in AppGallery Connect.

Generate a Signing Certificate by referring here.

Then you’ll need to export the SHA256 Fingerprint by using keytool provided by JDK. You can find detailed steps here.

Adding Fingerprint Certificate to AppGallery Connect

On the application information page of the project, click to add your SHA256 fingerprint. Then click ✓ to save fingerprint.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094144.16034522988837344924927496706951:50511019062808:2800:3F0D6DC36159569F98FC956A63ED5BF6E21EE562D89E1A4BD8B4CACAB6D220C4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094144.16034522988837344924927496706951:50511019062808:2800:3F0D6DC36159569F98FC956A63ED5BF6E21EE562D89E1A4BD8B4CACAB6D220C4.png[/IMG]

Enabling Account Kit Service

On the application information page, click Manage APIs and make sure Account Kit is enabled.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094300.90409198870169738780279192318690:50511019062808:2800:E928C6BC2BC2D7244C2302A2ED370CB66578D344584F63E322EE7880FA2772E2.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094300.90409198870169738780279192318690:50511019062808:2800:E928C6BC2BC2D7244C2302A2ED370CB66578D344584F63E322EE7880FA2772E2.png[/IMG]

Integrating HMS and Account Plugin to Flutter Project

Open pubspec.yaml file of your project and add Huawei Account Plugin as a dependency.

dependencies:
flutter:
sdk: flutter
huawei_account: ^5.0.0+300

Download agconnect-services.json file from application information page.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094839.23016154915262952401097740590090:50511019062808:2800:ADC7AF2AEB4399F1C6E547CFA285684788719DCA2F1AB04BD7369D60F69F7ED9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094839.23016154915262952401097740590090:50511019062808:2800:ADC7AF2AEB4399F1C6E547CFA285684788719DCA2F1AB04BD7369D60F69F7ED9.png[/IMG]

Move the file to the android/app directory with the signing certificate file you’ve created.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094901.84773189645296960546620956091082:50511019062808:2800:3ACD752D50C326EF25B02917B6EB683BCA6D05F04901C177846B1D7493D5F741.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922094901.84773189645296960546620956091082:50511019062808:2800:3ACD752D50C326EF25B02917B6EB683BCA6D05F04901C177846B1D7493D5F741.png[/IMG]

Open the android/build.gradle file and configure the Maven repository address and agconnect plugin for the HMS Core SDK.

buildscript {
repositories {
// Other repositories
maven { url 'https://developer.huawei.com/repo/' }
}
dependencies {
// Other dependencies
classpath 'com.huawei.agconnect:agcp:1.3.1.300'
}
}
allprojects {
repositories {
// Other repositories
maven { url 'https://developer.huawei.com/repo/' }
}
}

Add the AppGallery Connect plugin to your app level build.gradle file.

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

Then add the signing configurations in the same file. Don’t forget that your applicationId must be same as the one you created in AppGallery Connect. Also change your minSdkVersion to 19.

android {
defaultConfig {
applicationId "<application_id>"
minSdkVersion 19
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
signingConfigs {
config {
storeFile file('<keystore_file>')
storePassword '<keystore_password>'
keyAlias '<key_alias>'
keyPassword '<key_password>'
}
}
buildTypes {
debug {
signingConfig signingConfigs.config
}
release {
signingConfig signingConfigs.config
}
}
}

Now we’re done with the integration part and ready to use the Account Plugin in our application.

Using Huawei Account Kit Flutter Plugin

Setting Up The Authorization

Account Plugin allows you to customize the authorization. You can request users to authorize their email addresses, profile information or access tokens. To accomplish that, you can use AuthParamHelper class.

Create an instance of AuthParamHelper class. Then set the parameters you want to customize.

AuthParamHelper helper = new AuthParamHelper();
// You can set many options by using cascades
helper..setEmail()..setRequestCode(8888);

Signing In

Now we’re going to call signIn method through HmsAccount class and pass our helper instance to it. In return, this method gives us the Huawei Id information through AuthHuaweiId class.

try {
// Make sure you handle the possible exceptions.
final AuthHuaweiId authHuaweiId = await HmsAccount.signIn(helper);
} on Exception catch (e) {
print(e.toString());
}

As this method is triggered, an authorization screen shows up. After clicking the login button, you can see the Huawei Id information is received.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922095119.08918341316987761883753910019496:50511019062808:2800:6CD8B2520A71356923835C0E9264D8C029BA913AC982ED86DC01D4F9D64F1FFD.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922095119.08918341316987761883753910019496:50511019062808:2800:6CD8B2520A71356923835C0E9264D8C029BA913AC982ED86DC01D4F9D64F1FFD.gif[/IMG]

Account Plugin has signOut method which clears the user’s Huawei Id information when it comes to sign out from an app. But the information is not permanently deleted.

try {
// Make sure you handle possible exceptions.
final bool result = await HmsAccount.signOut();
} on Exception catch (e) {
print(e.toString());
}

Revoking Authorization

After signing in for the first time, when users try to sign in again, the authorization screen will not show up unless users revoke it. Huawei Id information will be received directly. Once the authorization is revoked, logged id information will be deleted. And the authorization screen will be shown on another login attempt.

try {
// Make sure you handle possible exceptions.
final bool result = await HmsAccount.revokeAuthorization();
} on Exception catch (e) {
print(e.toString());
}

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922095334.91083210541038863171866124468317:50511019062808:2800:4D18329BB2A631E00575DBB9B3F77863187A94A0F09508F854ABEE571FE51AA3.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/006/578/685/2640852000006578685.20200922095334.91083210541038863171866124468317:50511019062808:2800:4D18329BB2A631E00575DBB9B3F77863187A94A0F09508F854ABEE571FE51AA3.gif[/IMG]

Sms Verification

One of the options that Account Plugin provides us is the sms verification. This service can catch sms messages in certain formats. Unlike the other authorization methods, you should be able to send sms messages for this special service.

Obtaining Application’s Hash Code

To catch upcoming sms messages, we need to know the hash code value which is unique in each app. Account Plugin provides obtainHashcode method to get that.

try {
// Make sure you handle possible exceptions.
final String hashcode = await HmsAccount.obtainHashCode();
} on Exception catch (e) {
print(e.toString());
}

Sending Sms Messages

The messages you’ll send should be as follows:

prefix_flag short message verification code is ****** hash_code

prefix_flag indicates the prefix of an SMS message, which can be <#>, [#], or \u200b\u200b. \u200b\u200b are invisible Unicode characters.

short message verification code is indicates the content of an SMS message, which is user-defined.

****** indicates the verification code.

Receiving Sms Messages

Account Plugin comes with smsVerification method to listen and catch the right formatted messages. Once it is called, the app starts listening. Waits for the messages for five minutes before timing out. Returns the message or error code depending on the situation.

HmsAccount.smsVerification(({errorCode, message}){
if (message != null) {
// Use the message
} else {
print("Error: $errorCode");
}
});

It’s all done. Now you’re ready to use Account Plugin in your Flutter applications.

Conclusion

Huawei Account Kit is such an easy and secure way to carry out authorization processes, along many other kits with powerful solutions. I think these services will keep providing a nice experience for both developers and users as they get bigger and better.

You can check some other articles about Hms Core Plugins below, and feel free to ask any question about this article in the comments.