How to use Signing In and Silently Signing In with Huawei Account Kit?

Ismail Oguzhan Ay
Huawei Developers
Published in
6 min readNov 2, 2020

Hello everyone, today we will talk about handling the signing in processes with using Huawei Account Kit. With the Huawei Account Kit, we can manage the signing in and authorize scenarios easily. We will implement a demo application which uses authorization code and silent signing in use case for login scenario.

Functions

One-click sign-in

Signs HUAWEI ID holders into your app without manual login or verification.

Easy SMS verification

Enables your app to automatically input verification codes from SMS messages.

Fast user authorization

Helps you quickly get users’ authorization to access their information.

Service Features

Quick and standard

Connects your app to the Huawei ecosystem, allowing users to sign in to your app using their HUAWEI IDs from a range of devices, such as mobile phones, tablets, and smart screens.

Massive user base and global services

Serves more than 190 locations worldwide, and supports more than 70 languages, with up to 900 million users. Users can quickly sign in to your apps through their HUAWEI IDs.

Secure, reliable, and compliant with international standards

Complies with international standards and protocols such as OAuth 2.0 and Open ID Connect, and supports two-factor authentication to ensure high security.

Before starting the demo application review, there are important details about the Huawei Account Kit below.

1. How Does the Account Kit SDK Work?

  1. Register on HUAWEI Developers and apply for an app ID.
  2. Integrate the Account SDK into your app.
  3. Use your app to call the Account SDK to display the HUAWEI ID sign-in page and authorization page. On the authorization page, the user can determine which permissions to be granted to your app.
  4. Use your app to call the Account SDK to access the APIs of the permissions.

2. What Permissions Are Required for Using the SDK?

  • android.permission.ACCESS_NETWORK_STATE: Checks the network connection status.
  • android.permission.ACCESS_WIFI_STATE: Obtains the Wi-Fi connection status and information about Wi-Fi hotspots.

3. What are the maximum lengths of access_token and refresh_token?

  • The lengths of access_token and refresh_token are related to the information encoded in the tokens. Currently, access_token and refresh_token contains a maximum of 1024 characters.

4. Is there a limit on the frequency for calling the API for obtaining the access token?

  • This API can be called by an app up to 10,000 times within one hour. If the app exceeds the limit, it will fail to obtain the access token.

What is the difference between silent sign-in and normal sign-in?

What is the difference between signing a HUAWEI ID out of an app and revoking HUAWEI ID authorization to an app?

The sign-out of a HUAWEI ID does not affect the authorization, and the authorization page will not be displayed next time when you use the ID to sign in to the app no matter in normal or silent mode.

However, if HUAWEI ID authorization is revoked from an app, error code 2002 will be reported if you use the ID to sign in in silent mode and the authorization page will be displayed if you use the ID to sign in in normal mode.

Development Process

We need to follow some steps for the integration of Huawei Account Kit.

Huawei Account Kit Development Process Flow
  1. We need to register as a developer account in AppGallery Connect.
  2. We create an application and configuring app information in AppGallery Connect.
  3. After the configuration in AppGallery Connect, let’s integrate the Huawei Account Kit into our demo application.

a. We need to get agconnect-services.json file for configurations from AppGallery Connect. Then, we add it into our application project level under the app folder.

b. After that, we need to add dependencies into gradle files.

Now, we need to sync our gradle files.

Let’s start the coding !

We have 2 scenarios, one of them is for signing in first time with authorization code and the other one is silent signing in for checking authorize for each application starts with splash screen. So we have 2 different activities for handling signing in use case scenarios.

Signing with Authorization Code

After the UI fixing, we used Huawei id auth button component here. Please check this link for getting more information about usage the Huawei ID signing in button.

When we click the Huawei ID signing in button, it needs the HuaweiIdAuthParams and create a service with authParams. Then, we called startActivityForResult() method in Huawei ID signing in button click with service and HUAWEI_AUTH_CODE constant. The constant (HUAWEI_AUTH_CODE) uses for handling the requestCode in onActivityResult() method. When the user clicks log in with Huawei ID button, the app needs to authorize and login operations from the user.

In onActivityResult() method, we create a task for handling HuaweiIdAuthManager result. If the result is successful, we can get account data from authHuaweiIdTask.result parameter. According to example, we get the displayName and avatar uri with defining a value which is huaweiAccount.

Now, we completed the login use case scenarios with using authorization code mode a Huawei ID and we had authorization from the user. Let’s go with silent signing in use case.

Signing with Silent Signing In

With the silent signing in, authorization is required only at the first sign-in to your app using a HUAWEI ID. Subsequent sign-ins using the same HUAWEI ID does not require any authorization. So, we can manage that process in splash activity our use case demo application. When the application starts, the silent signing in method should check the users login status. Then, we can manage the application work flow logic with the silent signing in service response.

We create a method which is checkAuthWithSilentSignIn(). When we want to check user’s login status, this method checks the user’s status from HuaweiIdAuthService and it has callback for task listener results.

According to our use case, we should check the user’s login status in SplashActivity and manage to work flow in our demo application. We called the checkAuthWithSilentSignIn() method in splash activity, then navigate to splash activity to another activity by silent signing in result callback. In this way, we implemented silent signing in into our demo application.

In the end, we implemented a demo application for usages Huawei Account Kit. According to your application work flow, you can implement the Huawei Account Kit features. I hope this article will be useful for understanding and handling the use case scenarios.

Thank You ! ..

For more details, please check these useful links :

--

--