Few months ago I wrote an article about Using Native Facebook Login Button in Xamarin.Forms where I explained how to retrieve user access token using Facebook SDK. It is still a valid read and a good solution, however, recently I discovered that there is a shorter way to achieve almost the same thing.
In this article we will learn how to retrieve Facebook user access token, using a custom button (not native) with just a few lines of code. I assume that you know how to create a Facebook application and configure your Android and iOS projects. If I am mistaken please refer to my previous article.
There is an open source project Facebook Client Plugin that allow you to login, share and query Facebook using static API. It has a good documentation and good well known developers behind it. As you may already understand we are going to use it to retrieve user access token from Facebook in our project.
Here is the recipe:
- Create and configure Facebook app (more info here or here).
Plugin.FacebookClientNuGet package to .NET Standard and platform targeting projects.
- Configure platform targeting projects (more info here or here).
- Add a custom Facebook login button to your Page/View.
- Bind a command to the previously created button with the next code:
if(fbLoginResponse.Status == FacebookActionStatus.Completed)
// TODO: Use the fb access token
// Something went wrong
That should be it!
You could create a wrapper interface and class to make this code testable, to be able to inject it as a dependency and to adjust it to your needs. This solution might be less configurable, however, the integration is very easy and the amount of code is miserable compare to the previous solution.
Big thanks to CrossGeeks!