In app Authentication using Google accounts with Unified sign-in from AppGallery Connect
Tags: android, huawei, appgallery
Published: 2021-11-24 14:49:20

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](https://developers.google.com/identity/sign-in/android/start-integrating) 

Navigate to your [Google API&Services](https://console.cloud.google.com/projectselector2/apis/credentials?pli=1&supportedpurview=project) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jf95hxcg717q9cdztb6.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gckg5cz9q6te48k8a2ay.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ivkveeodgrqc0jdh2q26.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/po39wy7iqtokeztzoj0l.png)
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.
{% gist https://gist.github.com/devwithzachary/83475e7d3ee0f9b50d7f41c9008e6fd4 %}

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx7545vnlachorwi5aqn.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79i4fbeik76c97uk21se.png)
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](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#7) to view more details.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/96mlspr22wr7hnee7tnj.png)
4. Configure the generated SHA256 certificate fingerprint in AppGallery Connect. (If you do not perform this step, error 6003 is reported.)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b692m6bcupaqntnnef8z.png)
 
## Configuring the Android service
1. Open the project-level build.gradle file and configure the Maven repository address and other parameters, as follows:
{% gist https://gist.github.com/devwithzachary/6b5cb9afaef4448dace80df0082dd419 %}
2. Open the app-level build.gradle file and configure the SDK, app plug-in address and other parameters, as follows:
{% gist https://gist.github.com/devwithzachary/a82b33cf75351b967548199e27732c7c %}
3. Example sign in code:
{% gist https://gist.github.com/devwithzachary/45388f66ec58c0ce0f01ed7d3561d2a6 %}

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdhrzf3mmy4gn1yjpn4n.png)
After completing authorization and sign-in, you can view the following logs, which indicate the authentication and sign-in are successful.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0zgshc893ckbcm4p33b.png)
 
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](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-getstarted-0000001053053922#ZH-CN_TOPIC_0000001144209729__section20242120132219)
[Google sign-in integration in Auth Service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-google-0000001053372648)