Implement Huawei AppGallery Auth Service in your Xamarin Android app with Facebook Login support
Tags: android, xamarin, huawei, facebook
Published: 2022-02-03 14:45:30

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

Next make sure you have enabled the [Auth Service](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-android-getstarted-0000001053053922) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qoffdg2w5i54hpru8bmy.png)

Right-click your project and choose **Manage NuGet Packages**.
![Right click menu with Manage NuGet Packages highlighted](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tx9wcmxwx5s1jzca0vc.png)

Search for the **Huawei.Agconnect.Auth** package on the displayed page and install it.
![NuGet search result for Huawei.Agconnect.Auth](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr5ft4ny5pr3o2pd9jqr.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gpbvccmjn60i3u9usrpy.png)
 
Create a new class named **HmsLazyInputStreams.cs**, and implement the following code to read the JSON file.

{% gist https://gist.github.com/devwithzachary/893d7f4ccf3d7bbe091bb076eb15276e %}

Right-click your project and choose **Properties**. Click **Android Manifest** on the displayed page and set a package name
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gg9isq4chiow0qt3t7qp.png)

Follow the process that Facebook describes to setup your application with their platform. Details can be found [here](https://developers.facebook.com/docs/facebook-login/android) 

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

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

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

The Facebook Callback then looks like:

{% gist https://gist.github.com/devwithzachary/785a861395682d59cf798e7f9dfb76b0 %}

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

{% gist https://gist.github.com/devwithzachary/99cdac54231832159c43d77949e9fef1 %}

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.