In app Authentication using Facebook accounts with Unified sign-in from AppGallery Connect
Tags: android, huawei, appgallery, facebook
Published: 2021-12-03 14:45:00

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 Facebook 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 Facebook account? Let's take a look!

## Configuring the Facebook Login Environment
First, you'll need to configure the [Facebook Login](https://developers.facebook.com/docs/facebook-login/android) environment.

Select or create a Facebook app.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qq9dova9v4kcsvuvv9ze.png)

Edit your resource file. Copy the following code to the /app/res/values/strings.xml file of your Android project.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c8bqtu1anov0y0qfamyt.png)


Edit your manifest file. Add the following uses-permission element following the application element in the /app/manifest/AndroidManifest.xml file of your Android project. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0kb68ddv63vchi9z266.png)

Copy the following meta-data element to the application element.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54bb45q7ng7003owz0hk.png)

Associate your package name and default activity class with your app and click Save. Confirm the use of this package name (skip this if your app has not been released on Google Play). 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8t354sno4neg2enz7kc9.png)

Develop a key hash using the following command, and release it for your app.
If you haven't installed OpenSSL (openssl-for-windows) for your project, please go to [Google Code Archive](https://code.google.com/archive/p/openssl-for-windows/downloads) and download it as required.

{% gist https://gist.github.com/devwithzachary/13da0fe29a7252b079247f92df124095 %}

The command format is as follows:

keytool -exportcert -alias **YOUR_RELEASE_KEY_ALIAS**  -keystore *YOUR_RELEASE_KEY_PATH** | openssl sha1 -binary | openssl base64

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxgtukz6p7dme5z5i0hq.png)
The Facebook Login environment has now been successfully configured. Let's move on to the operations relevant to Auth Service.

## Enabling Auth Service

Sign in to AppGallery Connect, create a project and an app, and enable Auth Service. You'll need to enter the App ID and App Secret for your app when enabling Facebook under **Authentication modes**, which can be found under **Settings > Basic** on [Facebook for Developers](https://developers.facebook.com/apps#_=_). If they are not displayed, click the button following **App Secret** to show them.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73mqzm2lso87eew77jgm.png)
 
## Generating the Certificate Fingerprint for Your Android Project

If the demo project does not provide a Java KeyStore, go to Generate Signed Bundle or APK and click Create new.key store to create one
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uhrtaiuvq0k6yjtdcpec.png)
Generate an SHA-256 certificate fingerprint. Run the following command and enter the configured password to generate a SHA-256 certificate fingerprint.

{% gist https://gist.github.com/devwithzachary/a3feb0d528b0845315f95bcae4746bea %}

Open the app-level **build.gradle** file, and configure the certificate information for your project
Click [here](https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#7) to view the sample code.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dt08l672ck6rlkjzx12o.png)

Configure the generated SHA-256 certificate fingerprint in AppGallery Connect. (If you do not perform this step, error 6003 will be reported.)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnwpdu05cx9h37a134uo.png)
 
## Configure Your App
Open the project-level **build.gradle** file and configure information, including the Maven repository address. The code is as follows:

{% gist https://gist.github.com/devwithzachary/5ffe20f2b8872507f128742d9f69fe9c %}

Open the app-level **build.gradle** file and configure information, including the SDK information and app plug-in address. The code is as follows:

{% gist https://gist.github.com/devwithzachary/c146f3a18308b605f65e41c967b96614 %}

Add the code for implementing unified sign-in:

{% gist https://gist.github.com/devwithzachary/64b6c09a590b4b7cd6a6e3eaf19c887b %}

## Viewing the Result and Logs
Once you run your signed APK on an Android phone and call the FaceBookLogin method, you'll get the following page.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rsbj29vnkmrq103wdgpg.png)
 
You can tap CONTINUE AS LINKING and sign in to your app with your Facebook account. The following log information will be displayed.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfvzj2cwxzpd79kfl9de.png)

Compared with the traditional implementation mode, the unified sign-in has greatly simplified the development process. It is strongly recommended that you use the new mode.

For details about Auth Service, please refer to:

[Auth Service document for Android](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-getstarted-0000001053053922#ZH-CN_TOPIC_0000001144209729__section20242120132219)
[Guide for integrating Facebook account](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-facebook-0000001052932663#ZH-CN_TOPIC_0000001144049793__section16952720551)