[Huawei Developers] Ribhavmodi:
View attachment 5527985

Introduction

In this article, I will be integrating Huawei Account Kit in an Application.

Account Kit

Account Kit provides you with simple, secure and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authentication, users can just tap the Sign in with HUAWEI ID button to quickly and securely sign in to your app with their HUAWEI IDs.

Use Case

The Account Kit will be used to log in into a Banking Application which can perform basic functions such as Enter Pin, Withdraw Money, Deposit Money and Check Balance.

Requirements

1. Any operating system (MacOS, Linux and Windows).

2. Must have a Huawei phone with HMS 4.0.2.300 or later.

3. Must have a laptop or desktop with Android Studio, Jdk 1.8, SDK platform 26 and Gradle 4.6 installed.

4. Minimum API Level 21 is required.

5. Required EMUI 9.0.0 and later version devices.

Integrate HMS Dependencies

1. First register as Huawei developer and complete identity verification in Huawei developers website, refer to register a Huawei ID.

2. Create a project in android studio, refer Creating an Android Studio Project.

3. Generate a SHA-256 certificate fingerprint.

4. To generate SHA-256 certificate fingerprint. Choose View > Tool Windows > Gradle > Signingreport > SHA256 code.

Or use cmd as explained in SHA256 CODE

5. Create an App in AppGallery Connect.

6. Download the agconnect-services.json file from App information, copy and paste in android Project under app directory, as follows.

View attachment 5527987

7. Enter SHA-256 certificate fingerprint and click Save, as follows.

View attachment 5527991

8. Click Manage APIs tab and enable Account Kit.

View attachment 5527995

9. Add the below maven URL in build.gradle(Project) file under the repositories of buildscript, dependencies and allprojects, refer Add Configuration.

maven { url 'http://developer.huawei.com/repo/' }

classpath 'com.huawei.agconnect:agcp:1.6.0.300'

10. Add the below plugin and dependencies in build.gradle(Module) file.

apply plugin: 'com.huawei.agconnect'

// Huawei AGC

implementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'

// Account Kit

implementation 'com.huawei.hms:hwid:6.3.0.301'

11. Now Sync the gradle.

12. Add the required permission to the Manifestfile.xml file.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!--check wifi state-->

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Development

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<Button

android:id="@+id/sign_in"

android:layout_width="83dp"

android:layout_height="95dp"

android:text="Login with Huawei"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="0.542"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintVertical_bias="0.471" />

</androidx.constraintlayout.widget.ConstraintLayout>

Main Activity

class MainActivity : AppCompatActivity() {

var sign_in: Button? = null

var authParams: AccountAuthParams? = null

var service: AccountAuthService? = null

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

sign_in = findViewById<View>(R.id.sign_in) as Button

sign_in.setOnClickListener(object : OnClickListener() {

fun onClick(view: View?) {

authParams =

AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode()

.createParams()

service = AccountAuthManager.getService(this@MainActivity, authParams)

startActivityForResult(service.getSignInIntent(), 8888)

}

})

}

override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {

// Process the authorization result to obtain the authorization code from AuthAccount.

super.onActivityResult(requestCode, resultCode, data)

if (requestCode == 8888) {

val authAccountTask: jdk.internal.org.jline.utils.ShutdownHooks.Task<AuthAccount> =

AccountAuthManager.parseAuthResultFromIntent(data)

if (authAccountTask.isSuccessful()) {

// The sign-in is successful, and the user's ID information and authorization code are obtained.

val authAccount: AuthAccount = authAccountTask.getResult()

Log.i("MainActivity", "serverAuthCode:" + authAccount.getAuthorizationCode())

Log.i("MainActivity", "getDisplayName:" + authAccount.getDisplayName())

Log.i("MainActivity", "getResult:" + authAccountTask.getResult())

} else {

// The sign-in failed.

Log.e(

"MainActivity",

"sign in failed:" + (authAccountTask.getException() as ApiException).getStatusCode()

)

}

}

}

}

Cloud Debugging

Use Cloud Debugging in HMS Toolkit to debug the app on a real device.

To use Cloud Debugging, you need to sign in using a HUAWEI ID, complete identity verification, and then authorize the sign-in.

View attachment 5527997

Go to HMS > CloudDebugging.

You can use Available Devices. Select a device and click RUN.

Result

View attachment 5528003

Conclusion

In this article, we have learnt that how HMS Toolkit helps us to Connects users from a wide range of devices, including phones, tablets, and smart displays. It serves over 900 million users from 190+ countries and regions worldwide and supports 70+ Languages. It guarantees two-factor authentication, real-time risk prediction, and GDPR compliance.

Reference

Account Kit: Documentation