[Huawei Developers] shikkerimath:
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.

View attachment 5276947

(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.

View attachment 5276949

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.

View attachment 5276951

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

View attachment 5276953

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

View attachment 5276955

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.

View attachment 5276957

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.

View attachment 5276959

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

(a) Click Release.

View attachment 5276961

(b) Click Add project.

View attachment 5276963

(c) Create a project.

View attachment 5276965

(d) Click Add app.

View attachment 5276967

(e) Add the project.

View attachment 5276969

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

View attachment 5276971

5. Choose Account Kit.

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

View attachment 5276973

The following page will be displayed.

View attachment 5276975

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.

View attachment 5276977

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.

View attachment 5276979

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.

View attachment 5276981

Select the desired device model.

View attachment 5276983

View attachment 5276985

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.

<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.

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.

@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.

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.

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