[Huawei Developers] shikkerimath:
Introduction

In this article, we will cover how to verify Phone Number and Anonymous Account Login using Huawei Auth Service in Unity Project using Official Plugin (Huawei HMS Core App Services). AppGallery Connect provides a cloud-based Auth Service and SDKs to help you quickly build a secure and reliable user authentication system for your apps to verify user identity.

The AppGallery Connect Auth service supports multiple authentication methods and is seamlessly integrated with other server less services to secure user data based on simple rules that you have defined.

Key Functions​

Using the AppGallery Auth Service SDK, you can integrate one or more of the following authentication methods into your app for achieving easy and efficient user registration and sign-in.

Self-owned account: Your self-owned account is used to support the access of your existing authentication system, so that your existing users can access other server less services in a secure manner.

Anonymous account: Anonymous accounts can be used to access your apps as visitors. The Auth service can assign user IDs to your app visitors, so that they can access other server less services in a secure manner. A visitor can be registered as a formal user and retain the original user ID to ensure service continuity.

Third-party accounts: AppGallery Connect allows user identity to be verified by third-party authentication services. The AppGallery Auth Service SDK supports the following accounts for user identity verification:

1. HUAWEI account

2. HUAWEI Game Service account

3. Phone number

4. Email account

5. WeChat account

6. Weibo account

Development Overview

You need to install Unity software and I assume that you have prior knowledge about the unity and C#.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

Java JDK installation package.

Unity software installed.

Visual Studio/Code installed.

HMS Core (APK) 4.X or later.

Follows the steps.

1. Create Unity Project.

Open Unity Hub.

Click NEW, select 3D, Project Name and Location.

Click CREATE, as follows

View attachment 5299149

2. Click Asset Store, search Huawei HMS Core App Services and click Import, as follows.

View attachment 5299151

3. Once import is successful, verify directory in Assets > Huawei HMS Core App Services path, as follows.

View attachment 5299153

4. Choose Edit > Project Settings > Player and edit the required options in Publishing Settings, as follows.

View attachment 5299155

5. Generate a SHA-256 certificate fingerprint.

To generating SHA-256 certificate fingerprint use below command

keytool -list -v -keystore D:\Unity\projects_unity\file_name.keystore -alias alias_name

View attachment 5299157

6. Download agconnect-services.json and copy and paste to Assets > Plugins > Android, as follows.

View attachment 5299159

7. Choose Project Settings > Player and update package name.

View attachment 5299161

8. Open LauncherTemplate.gradle and add below line.

apply plugin: 'com.huawei.agconnect'

implementation 'com.huawei.agconnect:agconnect-auth:1.4.2.301'
implementation 'com.huawei.hms:base:5.2.0.300'
implementation 'com.huawei.hms:hwid:5.2.0.300'

9. Open AndroidManifest file and add below permissions.

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

10. Open "baseProjectTemplate.gradle" and add lines, as follows.

classpath 'com.huawei.agconnect:agcp:1.4.1.300'
maven {url 'https://developer.huawei.com/repo/'}

11. Open "mainTemplate.gradle" and add lines like shown below.

implementation 'com.huawei.agconnect:agconnect-auth:1.4.2.301'
implementation 'com.huawei.hms:base:5.2.0.300'
implementation 'com.huawei.hms:hwid:5.2.0.300'

12. Create MainActivity.java class inside Plugin > Android folder.

13. Follow the steps, as shown in image:

a. Assign Ads script to Canvas.

b. Select Button and add onclick event

c. Assign all button to button handler as per your requirements.

View attachment 5299165

14. Onclick Button Handler you find your script HMSAuthService (As per your script name) and attach method as per below screen shot.

View attachment 5299167

15. To build apk and run in device, choose File > Build Settings > Build for apk or Build and Run for run on connected device.

View attachment 5299171

Result

1. Click on Anonymous Login, send OTP and Verify button you can see below results.

View attachment 5299177

View attachment 5299179

2. Find the details in AppGallery Connect, as follows.

View attachment 5299181

View attachment 5299183

Tips and Tricks

Always use the latest version of the library.

Add agconnect-services.json file without fail.

Add SHA-256 fingerprint without fail.

Make sure dependencies added in build files.

Make sure that you enabled the auth service in AG-Console.

Make sure that you enabled the Authentication mode in Auth Service.

Conclusion

In this article, we have learnt integration of Huawei Auth Service-AGC anonymous account login and mobile number verification through OTP in Unity Game development. Auth Service provides secure and reliable user authentication system to your application.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Unity Auth Service Manual

Auth Service

Checkout in forum