DEV Community

Zachary Powell
Zachary Powell

Posted on

Implement Huawei AppGallery Auth Service in your Xamarin Android app with Facebook Login support

Xamarin (Microsoft) is a multi-system development platform for mobile services that many developers use. Many AppGallery Connect services now support Xamarin, including Auth Service. Here I'll explain on how to integrate Auth Service into your Xamarin.Android app and use the 3rd party auth support for Facebook.

Install the Xamarin environment and project setup.

You'll need to first download and install Visual Studio 2019.
Open Visual Studio and select Mobile development with .NET to install the Xamarin environment.

Visual Studios install screen

Next make sure you have enabled the Auth Service in AppGallery Connect.

Open Visual Studio, click Create a new project in the start window, select Mobile App (Xamarin.Forms), and set the app name and other required information.

Visual Studio create new project screen

Right-click your project and choose Manage NuGet Packages.
Right click menu with Manage NuGet Packages highlighted

Search for the Huawei.Agconnect.Auth package on the displayed page and install it.
NuGet search result for Huawei.Agconnect.Auth

Download the JSON service file from your AppGallery project and add it into the *Assets directory in your project.
Xamarin file structure showing agconnect-service.json file in Asserts folder

Create a new class named HmsLazyInputStreams.cs, and implement the following code to read the JSON file.

Right-click your project and choose Properties. Click Android Manifest on the displayed page and set a package name
Image description

Follow the process that Facebook describes to setup your application with their platform. Details can be found here

Finally the last thing we need to do is install the Xamarin Facebook login package.

Right-click your project and choose Manage NuGet Packages. Search for Xamarin.Facebook.Login.Android and install it.
Image description

Implement Facebook Auth

Create a login button within your application, this will use the below code to start the login process getting the specific requested account details from Facebook and process this using the call back.

The Facebook Callback then looks like:

In the OnSuccess method, we have obtained the access token and generated a credential for Facebook Login, and called SignInAsync to pass the credential for sign-in.

Finally, forward OnActivityResult to the callBackManager.

The result code 64206 is defined by Facebook in its Login SDK. It is used to identify the callback for Facebook Login when there are multiple callbacks in OnActivityResult. An interesting fact is that, the hexadecimal value of 64206 is 0xFACE, which is a little trick from Facebook.

Discussion (0)