[Huawei Developers] devwithzachary:
With the release of AppGallery Connect version 1.5.2 the Auth service now has full support for making use of unified sign-in with a Google account!

This new functionality makes AppGallery Connect Auth a great option for all of your app’s authentication needs both on Huawei devices and other Android devices.

So how do we go about using unified sign-in with a Google account? Let’s take a look!

Preparing the environment for Google Sign-in​

Details: Google Identity

Navigate to your Google API&Services console.

Open the API&Services and click My Project on the upper left corner. You can create one if no project exists.

Then, click CREATE CREDENTIALS to create an OAuth client ID for your project. If you haven’t already configured the consent screen for this project, follow the on-screen instructions to configure it.

After the configuration is complete, you can navigate back to Credentials > Create CREDENTIALS > OAuth client ID to continue. Select Android as the application type and configure the package name and the SHA-1 certificate fingerprint of the JKS file. The SHA-1 certificate fingerprint here is the SHA-1 value generated in step 3. (If this step is not performed, the timeout error code 10 will be displayed when you integrate Google sign-in.)

After the Android OAuth client has been created, go to the settings page, click Web client (auto-created by Google Service) and copy Client ID and Client secret which are needed for integrating Google sign-in.

Add a resource file. Set app_id and client_id in the /app/res/values/strings.xml of the Android project. The value of client_id is Client ID in the previous step and the value of app_id is the string of numbers that starts with the Client ID.

<string name="google_app_id">81792xxxx258</string>
<string name="google_client_id">81792xxxx2258-m8crjg1xxxxxxx3kufvbji0amlp.apps.googleusercontent.com</string>

Now that everything is ready, let’s move on to enabling Auth Service.

Enabling Auth Service​

Sign in to AppGallery Connect, create a project and an app, and enable Auth Service. You need to enter the Client ID and Client secret when enabling Google account authentication.

As covered in the previous step, these two can be found by going to Google API&Services > Credentials > OAuth 2.0 Client IDs and opening Web client (Auto-created for Google Sign-in).

Generating the certificate fingerprint for your Android project​

If the app does not have a JKS certificate, go to Generate Signed Bundle or APK and click Create a new key store to create one. The generated JKS certificate is the JKS file used in Step 1.

Generate an SHA-256 certificate fingerprint by running the following command and entering the configured password. The SHA1 value must be identical to that configured in step 1.

keytool -list -v -keystore you_path\AuthDemo-Union\app\authdemounion.jks

Configure the corresponding Gradle file, open the app-level build.gradle file, and configure the certificate. Click here to view more details.

Configure the generated SHA256 certificate fingerprint in AppGallery Connect. (If you do not perform this step, error 6003 is reported.)

Configuring the Android service​

Open the project-level build.gradle file and configure the Maven repository address and other parameters, as follows:

buildscript {
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' } // config this maven
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.huawei.agconnect:agcp:1.5.1.300' // config this path
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' } // config this maven
}
}

Open the app-level build.gradle file and configure the SDK, app plug-in address and other parameters, as follows:

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect' // config this apply

android {…}

dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

implementation "com.huawei.agconnect:agconnect-auth-google:1.5.2.201" // config this SDK
implementation 'com.huawei.agconnect:agconnect-auth:1.5.2.201' // config this SDK
}

Example sign in code:

private void GoogleIDLogin(){
Log.i("AuthDemo", "start:" );
AGConnectAuth.getInstance().signIn(this, AGConnectAuthCredential. Google_Provider)
.addOnSuccessListener(new OnSuccessListener<SignInResult>() {
@Override
public void onSuccess(SignInResult signInResult) {
// onSuccess
AGConnectUser user = signInResult.getUser();
Log.i("AuthDemo", "success:" + user);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// onFail
Log.i("AuthDemo", "failed:" + e.getMessage());
}
});
}
// Lifecycle required by the unified channel.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AGConnectApi.getInstance().activityLifecycle().onActivityResult(requestCode, resultCode, data);
}

Result​

Once you run the app that has been configured with the signature file in an Android phone and call the GoogleIDLogin method, the following page will appear.

After completing authorization and sign-in, you can view the following logs, which indicate the authentication and sign-in are successful.

Compared with the traditional authentication mode, unified sign-in has significantly improved development, and for this reason, we strongly recommend that you leverage this function.

References:

Auth Service for Android

Google sign-in integration in Auth Service