[Huawei Developers] John Wo:
More information like this, you can visit HUAWEI Developer Forum​

Original link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201346103655580149&fid=0101188387844930001

Introduction

This article is based on Huawei Mobile Services application. I have developed Trip Booking Android app. We can provide the solution for HMS based multiple kits such as Account Kit, Huawei Ads, Huawei Map, and Huawei Analysts to use in Trip Booking. So users can book any trip.

In this application, users can plan trips and advance books. It will provide the ongoing trip cities wise with weather forecasting so that users can easily plan a trip and book trip and also can find popular cities with budget hotels and much more.

Service Process

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904134518.50607545317090861072560835432900:50510916013339:2800:08BC4DEC7556F2B183E8369DCE92A61CB9C21DE79C432CE0CAEF43CCA0C9FD0E.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904134518.50607545317090861072560835432900:50510916013339:2800:08BC4DEC7556F2B183E8369DCE92A61CB9C21DE79C432CE0CAEF43CCA0C9FD0E.png[/IMG]

The process is described as follows:

1. A user signs in to your app using a HUAWEI ID.

2. Your app sends a sign-in request to the Account SDK.

3. The Account SDK brings up the user sign-in authorization interface, explicitly notifying the user of the content to be authorized based on the authorization scope contained in the sign-in request.

4. After the user authorizes your app to access the requested content, the Account SDK returns the authorization code to your app.

5. Based on the authorization code, your app obtains the access token, refresh token, and ID token from the HUAWEI Account Kit server.

6. Based on the access token, your app server obtains openId of the user from the HUAWEI Account Kit server.

7. If the access token or ID token has expired, your app server obtains a new access token or ID token using the refresh token.

Prerequisite

1. A computer (desktop or laptop).

2. A Huawei phone used for running the app with HMS Core (APK) 3.0.0.300 or later.

3. A data cable used for connecting the computer to the Huawei phone.

4. Android Studio 3.0 or later.

5. Java SDK 1.7 or later.

6. HMS Core SDK 4.0.0.300 or later.

7. Must have Huawei Developer Account.

Things Need To Be Done

1. Create an app in AppGallery Connect and enable the service.

2. Create an Android Studio project.

3. Start development with kit integration inside the application.

4. Launch the application.

Create a project on AppGalleryConnect portal

1. Navigate to AppGalleryConnect, create a new project.

2. Enter all details regarding your application, enable Account Kit API, after that download the configuration JSON file, and then add into your android studio project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904134649.81128795748307192772236768515950:50510916013339:2800:811887150AC254BCEC42B2A7FBDF08DE3356F2C1C9E8D9AC9AD1B7BFE0A43A66.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904134649.81128795748307192772236768515950:50510916013339:2800:811887150AC254BCEC42B2A7FBDF08DE3356F2C1C9E8D9AC9AD1B7BFE0A43A66.png[/IMG]

Create a project in Android Studio:

Navigate to android studio and create a new android project, provide all details of your project, and then add AGC and Account kit SDK dependencies.

1. Add maven URL and add following AppGalleryConnect classpath.

buildscript {
repositories {
maven {url 'http://developer.huawei.com/repo/'}
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.huawei.agconnect:agcp:1.0.0.300'

}
}
allprojects {
repositories {
maven {url 'http://developer.huawei.com/repo/'}
google()
jcenter()

}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

2. Add the following dependencies in app module based Gradle file, then sync your project.

implementation "com.google.code.gson:gson:2.8.5"
implementation('com.huawei.hms:hwid:4.0.4.300')
implementation "com.squareup.okhttp3:okhttp:3.14.2"
implementation 'com.squareup.okio:okio:1.14.1'

Start development with kit integration inside the application

I have created the following package inside the project.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904135047.33036943607997463779367389561613:50510916013339:2800:0CA217B31F6D9CC4108F6518E9FDA11880AD0883E25642E8E8EDCA7F49A93263.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904135047.33036943607997463779367389561613:50510916013339:2800:0CA217B31F6D9CC4108F6518E9FDA11880AD0883E25642E8E8EDCA7F49A93263.png[/IMG]

LoginActivity

In this screen, I have integrated login functionality with Huawei Id.

Which cover the following APIs

1. Call the HuaweiIdAuthParamsHelper.setAuthorizationCode API to send an authorization request.

HuaweiIdAuthParams authParams = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setAuthorizationCode().createParams();

2. Call the getService API of HuaweiIdAuthManager to initialize the HuaweiIdAuthService object.

HuaweiIdAuthService service = HuaweiIdAuthManager.getService(MainActivity.this, authParams);

3. Call the HuaweiIdAuthService.getSignInIntent method to bring up the HUAWEI ID sign-in authorization interface.

startActivityForResult(mAuthManager.getSignInIntent(), Constant.REQUEST_SIGN_IN_LOGIN);

4. Process the sign-in result after the authorization is complete.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constant.REQUEST_SIGN_IN_LOGIN) {
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, huaweiAccount.getDisplayName() + " signIn success ");
Log.i(TAG, "AccessToken: " + huaweiAccount.getAccessToken());

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("user", huaweiAccount.getDisplayName());
startActivity(intent);
this.finish();

} else {
Log.i(TAG, "signIn failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}
if (requestCode == Constant.REQUEST_SIGN_IN_LOGIN_CODE) {
//login success
Task<AuthHuaweiId> authHuaweiIdTask = HuaweiIdAuthManager.parseAuthResultFromIntent(data);
if (authHuaweiIdTask.isSuccessful()) {
AuthHuaweiId huaweiAccount = authHuaweiIdTask.getResult();
Log.i(TAG, "signIn get code success.");
Log.i(TAG, "ServerAuthCode: " + huaweiAccount.getAuthorizationCode());

} else {
Log.i(TAG, "signIn get code failed: " + ((ApiException) authHuaweiIdTask.getException()).getStatusCode());
}
}
}

MainActivity

In this screen, I have integrated all fragments which are related to application and also added Logout functionality.

public class MainActivity extends AppCompatActivity {

private static String TAG = MainActivity.class.getName();
private AppBarConfiguration mAppBarConfiguration;
private HuaweiIdAuthService mAuthManager;
private HuaweiIdAuthParams mAuthParam;

private void setUser(String name) {
NavigationView navigationView = findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView navUsername = headerView.findViewById(R.id.txt_user_name);
navUsername.setText(name);
}

private void logOut() {
mAuthParam = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
.setIdToken()
.setAccessToken()
.createParams();
mAuthManager = HuaweiIdAuthManager.getService(this, mAuthParam);
Task<Void> signOutTask = mAuthManager.signOut();
signOutTask.addOnSuccessListener(aVoid -> {
Log.i(TAG, "signOut Success");
Intent intent = new Intent(this, LoginScreen.class);
startActivity(intent);
this.finish();
})
.addOnFailureListener(e -> Log.i(TAG, "signOut fail"));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_logout:
logOut();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

}

Launch the application

Let us launch our application, see the result

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904135527.28633201540553640575282213248431:50510916013339:2800:FCBE7ABF98221BDE02AB22D7337808C467F3F8B53E684CBFBD710B034D825363.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904135527.28633201540553640575282213248431:50510916013339:2800:FCBE7ABF98221BDE02AB22D7337808C467F3F8B53E684CBFBD710B034D825363.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904135244.77973532355107947304625975319118:50510916013339:2800:79F6D505F3956809ED4ABB420E1CA832A1D46EFCEC399B140AA0E2B2D195F770.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/768/541/2640091000001768541.20200904135244.77973532355107947304625975319118:50510916013339:2800:79F6D505F3956809ED4ABB420E1CA832A1D46EFCEC399B140AA0E2B2D195F770.jpg[/IMG]

If you have any doubts or queries. Leave your valuable comment in the comment section and do not forget to like and follow me.

References

https://developer.huawei.com/consumer/en/hms/huawei-accountkit