DEV Community

Zachary Powell
Zachary Powell

Posted on

4

In app Authentication using Google accounts with Unified sign-in from AppGallery Connect

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.

  1. Open the API&Services and click My Project on the upper left corner. You can create one if no project exists. Image description
  2. 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. Image description
  3. 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.) Image description
  4. 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. Image description
  5. 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>
    view raw string.xml hosted with ❤ by GitHub

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).
Image description

Generating the certificate fingerprint for your Android project

  1. 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. Image description
  2. 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
  3. Configure the corresponding Gradle file, open the app-level build.gradle file, and configure the certificate. Click here to view more details. Image description
  4. Configure the generated SHA256 certificate fingerprint in AppGallery Connect. (If you do not perform this step, error 6003 is reported.) Image description

Configuring the Android service

  1. 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
    }
    }
    view raw build.gradle hosted with ❤ by GitHub
  2. 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
    }
    view raw build.gradle hosted with ❤ by GitHub
  3. 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.
Image description
After completing authorization and sign-in, you can view the following logs, which indicate the authentication and sign-in are successful.
Image description

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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (3)

Collapse
 
osamasayed585 profile image
Osama Sayed • Edited

I followed all the previous steps but I have a problem
this is option is not enabled
dev-to-uploads.s3.amazonaws.com/up...
As shown here
developer.huawei.com/consumer/en/d...

I hope you will make a video explaining this article, we will be very thankful

Collapse
 
osamasayed585 profile image
Osama Sayed

Is this be with just Huawei?

Collapse
 
devwithzachary profile image
Zachary Powell

Nope! It works on all Android devices, iOS and other platforms as well! checkout developer.huawei.com/consumer/en/d... for everything supported

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more