【Integration Guide】 HUAWEI Account Kit Demo

Search This thread

shikkerimath

Senior Member
Nov 30, 2020
65
21
In the previous post, we learned the advantages of Account Kit: one-click sign-in authorization in any scenario, secure and reliable services, and convenient integration, as well as giving apps access to the global HUAWEI ID base of potential users. A QR code is also provided at the end of the post for you to download the demo app to experience HUAWEI ID sign-in authorization. The demo has integrated three Account Kit APIs (four in total), and is easy to develop. In this article, we'll show you the demo development to get you familiar with Account Kit.



Before you start, use a browser to scan the QR code below to try the demo app.

1618223030826.png


(Note: The app may collect relevant information for user statistics.)

Preparing for Demo Development​

l Install Android Studio 3.5 or later.

l Install JDK 1.8 or later.

l Use SDK Platform 19 or later.

l Use Gradle 4.6 or later.

l If you haven't already, register as a developer on HUAWEI Developers.

l Download the sample code of the demo from GitHub.

Configuring the Running Environment (2 minutes)​

1. Use Android Studio to open the demo project.

2. Go to File > Settings > Plugins > Marketplace, enter HMS Toolkit in the search box, and click Install. Note: HMS Toolkit must be version 5.2.0.300 or later.

For details, please refer to Installing HMS Toolkit.

1618223045238.png


3. Create a package and rename it to something like com.hxb.account in the project. Then, copy the code in the com.huawei.hms.accountsample package to your package, and change the value of package and applicationId to your package name. (Do not use the existing package name in the demo project because it is already registered on HUAWEI AppGallery.)



First, create a package. Move the MainActivity class to your package so that you can search for related files easily during the build.

1618223059869.png


Change package in the AndroidManifest.xml file to com.hxb.account.

1618223074457.png


Change applicationId in the build.gradle file to com.hxb.account.

1618223086344.png


4. Go to HMS > Configuration Wizard to check the environment configuration. If you have not signed in with a HUAWEI ID, Toolkit will first prompt you to do so.

1618223097957.png


On the Configuration Wizard page displayed, you are prompted that no app corresponding to the package name was detected under the signed-in HUAWEI ID.

1618223110818.png


Click Link to go to AppGallery Connect and create an app manually:

(a) Click Release.

1618223123643.png


(b) Click Add project.

1618223138027.png


(c) Create a project.

1618223152931.png


(d) Click Add app.

1618223170147.png


(e) Add the project.

1618223185654.png


After creating, go back to the Configuration Wizard page and click Retry. This time, the check will be successful.

1618223199420.png


5. Choose Account Kit.

On the Configuration Wizard page, click Add Kits and choose Account Kit.

1618223212835.png


The following page will be displayed.

1618223224497.png


6. Choose a certificate. You can choose Use Android debug certificate and click Generate to generate a certificate fingerprint, as shown in the following figure.

1618223237359.png


7. Click Next to automatically complete other configurations including toggling on the Account Kit switch in AppGallery Connect, configuring the signing certificate fingerprint, downloading the agconnect-services.json file to the project directory, configuring obfuscation scripts, and adding build dependencies and APK fingerprint required for integrating the Account SDK to the build.gradle file. Once these configurations are completed, the following page will be displayed. If you run into errors, please refer to the corresponding guide on the page.

1618223251459.png


Packaging and Testing the Demo on Remote Real Device Provided by Toolkit​

After configuration, go to HMS > Cloud Debugging to package and test the demo app.

1618223264817.png


Select the desired device model.

1618223276291.png


1618223283395.png


Click Run to test the demo app.

Key Code for Demo Development​

1. UI Design

Account Kit provides four main APIs for signing in, silently signing in, revoking authorization, and signing out. The demo app integrates the first three.

In the above figure, the Sign in with HUAWEI ID button is created with an encapsulated standard control. Please follow HUAWEI ID Sign-In Button Usage Rules when using the icon elements provided by Huawei.

Code:
<p style="line-height: 1.5em;"><com.huawei.hms.support.hwid.ui.HuaweiIdAuthButton
    
android
:layout_width
="wrap_content"
    
android
:layout_height
="wrap_content"
/>

1. Key Code for APIs

(1) Sign-in

Use case: Account Kit, compliant with OAuth 2.0 and OpenID Connect, supports sign-in in two modes: authorization code (for apps with their own servers only) and ID token modes. Select the mode you need.

Code:
private void signIn() {
    mAuthParam = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
          .setIdToken()
.setAccessToken()
.createParams();
    mAuthManager = AccountAuthManager.getService(AccountActivity.this, mAuthParam);
    startActivityForResult(mAuthManager.getSignInIntent(), Constant.REQUEST_SIGN_IN_LOGIN);
 }

As shown, setIdToken() means use the ID token mode for authorization. The other mode will show setAuthorizationCode() instead. Their difference will be described later. getSignInIntent() is the API for ID sign-in authorization.

Process the result after authorization.

Code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constant.REQUEST_SIGN_IN_LOGIN) {
// Successful sign-in.
// Obtain user information through parseAuthResultFromIntent.
Task<AuthAccount> authAccountTask = AccountAuthManager.parseAuthResultFromIntent(data);
if (authAccountTask.isSuccessful()) {
AuthAccount authAccount = authAccountTask.getResult();
Log.i(TAG, authAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken:\n" + authAccount.getAccessToken());
Log.i(TAG, "OpenId:\n" + authAccount.getOpenId());
Log.i(TAG, "Email:\n" + authAccount.getEmail());
Log.i(TAG, "UnionId:\n" + authAccount.getUnionId());


         // Download avatar by using AsyncTask.
NetService = new NetService(new URLPostHandler() {
@Override
public void PostHandler(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
textView.setText(authAccount.getDisplayName());
}
});
netService.execute(authAccount.getAvatarUriString());
} else {
Log.i(TAG, "signIn failed: " + ((ApiException)


  authAccountTask.getException()).getStatusCode());
}
}
}

(2) Silent sign-in

Use case: Authorization is required only on first sign-in to your app using a HUAWEI ID. Subsequent sign-ins using the same HUAWEI ID do not require any authorization.

Code:
private void silentSignIn() {
    Task<AuthAccount> task = mAuthManager.silentSignIn();
    task.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
       @Override
       public void onSuccess(AuthAccount authAccount) {
          Log.i(TAG, "silentSignIn success");
       }
    });
    task.addOnFailureListener(new OnFailureListener() {
       @Override
       public void onFailure(Exception e) {
          // If failed, use getSignInIntent.
          if (e instanceof ApiException) {
             ApiException apiException = (ApiException) e;
             signIn();
          }
       }
});
 }

Call silentSignIn() to implement silent sign-in.



(3) Authorization revoking

Use case: To improve privacy security, users can unauthorize your app.

Code:
private void cancelAuthorization() {
Task<Void> task = mAuthManager.cancelAuthorization();
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {


         imageView.setImageDrawable(null);
textView.setText("");
Log.i(TAG, "cancelAuthorization success");
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.i(TAG, "cancelAuthorization failure:" + e.getClass().getSimpleName());
}
});
}

Call cancelAuthorization() to revoke authorization.

For more information about Account Kit, please visit:

l Development guide

l Codelab

l Video course (and the course for HMS Core 4.0.)



For more details, you can go to:

Our official website

Demo of Analytics Kit

Android SDK integration documentation

iOS SDK integration documentation

Web SDK integration documentation

Quick app SDK integration documentation

Checkout in forum
 
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone