[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 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 environment.

Select or create a Facebook app.

Edit your resource file. Copy the following code to the /app/res/values/strings.xml file of your Android project.

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.

Copy the following meta-data element to the application element.

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

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 and download it as required.

keytool -exportcert -alias authdemounion -keystore F:\1.test\4.Auth\AuthDemo-Union\app\authdemounion.jks | openssl sha1 -binary | openssl base64

The command format is as follows:

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

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. If they are not displayed, click the button following App Secret to show them.

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

Generate an SHA-256 certificate fingerprint. Run the following command and enter the configured password to generate a SHA-256 certificate fingerprint.

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

Open the app-level build.gradle file, and configure the certificate information for your project

Click here to view the sample code.

Configure the generated SHA-256 certificate fingerprint in AppGallery Connect. (If you do not perform this step, error 6003 will be reported.)

Configure Your App​

Open the project-level build.gradle file and configure information, including the Maven repository address. The code is as follows:

buildscript {
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' } // Configure this maven.
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath 'com.huawei.agconnect:agcp:1.5.1.300' // Configure this path.
// NOTE: Do not copy your dependencies in here, which should be included
// in the build.gradle files of each module.
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'http://developer.huawei.com/repo/' } // Configure this maven.
}
}

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

apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect' // Configure the apply plug-in.

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-facebook:1.5.2.201" // Configure this SDK.
implementation 'com.huawei.agconnect:agconnect-auth:1.5.2.201' // Configure this SDK.
}

Add the code for implementing unified sign-in:

private void FaceBookLogin(){
Log.i("AuthDemo", "start:" );
AGConnectAuth.getInstance().signIn(this, AGConnectAuthCredential.Facebook_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());
}
});
}
// Activity lifecycle required by the unified sign-in.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AGConnectApi.getInstance().activityLifecycle().onActivityResult(requestCode, resultCode, data);

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.

You can tap CONTINUE AS LINKING and sign in to your app with your Facebook account. The following log information will be displayed.

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

Guide for integrating Facebook account