📜 ⬆️ ⬇️

Work with Facebook API from UWP applications


People, as a rule, creatures are very forgetful. In order not to force users to remember a new password when registering with your UWP application, you can use third-party platform accounts. At the same time, you can, without forcing you to fill in long questionnaires, to gain access to any useful information and user characteristics. The fact that the publication of content on social networks increases the conversion even I will not mention, it is understandable. If you want to figure out how to work with the most popular network in the world from the C # / XAML applications of UWP, without smoking manuals, then welcome under cat.

Linking a Facebook app with a UWP app


In order for the UWP application to work with the Facevook API, you need to create a Facebook application. This is done through the portal https://developers.facebook.com/apps/ by pressing the button "+ Add a new application"



We fill in the fields of the application name and contact address, and also classify the application by selecting a category.
')


Next we need the login feature via facebook



We build switches in such a way that at a minimum OAuth client authorization, OAuth web authorization, and OAuth authorization built into the browser are enabled



and select the platform by clicking on the platform settings



Get the window:



in which we select Application for Windows

Now you can make some settings, but they are not critical. But then you will need the application ID facebook.



What we really need to fill out is a bit lower:



The value of the Windows Store Security Identifier (SID) can be obtained in various ways.
A simpler way is from code using:

WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString(); 

This line will return us ms-app: // SID (ms-app: // will need to be removed)

The second method requires the application to be associated with the Store. In this case, you can go to the control panel , then go to App management of the application and select App identity. Value PackageSID this is what you need.



Working with the Facebook API using the Windows SDK for Facebook


We use the library from Microsoft with the difficult to pronounce abbreviated name winsdkfb . The full name is much easier to pronounce - Windows SDK for Facebook. This is the most convenient way to use the Facebook API from UWP apps. You can download and install it with NuGet

In C #, the XAML page code will add a namespace

 using winsdkfb; 

The authentication code is:

  string[] requested_permissions = {"public_profile", "email", "user_friends"}; FBSession session = FBSession.ActiveSession; session.WinAppId = WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString(); session.FBAppId = "229583744102151"; FBPermissions permissions = new FBPermissions(requested_permissions); FBResult result = await session.LoginAsync(permissions); if (result.Succeeded) { //    -   } else { //       facebook } 

The first line sets the permissions that the application will have access to. In the example, 3 permissions are mentioned, for which facebook does not request additional user confirmations.
The full list is available at the link Permissions Help: “Login via Facebook”
The two parameters you need to set for the connection are the already mentioned SID and the identifier of the created facebook application.

As a result, we get the following built-in window:



Knowing how to enter, you need to know how to exit. This can be done using similar code:

  FBSession sess = FBSession.ActiveSession; await sess.LogoutAsync(); 

Simple user data can be obtained from the session.User object.

  string username = session.User.Name; //   string locale = session.User.Locale; //   

What can be done useful? For example, you can add to your application the ability to post a message with the result / record of the game or an invitation to join

  PropertySet parameters = new PropertySet(); parameters.Add("title", " MP3"); parameters.Add("link", "https://www.microsoft.com/ru-ru/store/p/-mp3/9wzdncrdkwgv"); parameters.Add("description", "  -    "); parameters.Add("message", "    "); string path = "/" + session.User.Id + "/feed"; var factory = new FBJsonClassFactory(s => { return JsonConvert.DeserializeObject<FBReturnObject>(s); }); var singleValue = new FBSingleValue(path, parameters, factory); var fbresult = await singleValue.PostAsync(); if (fbresult.Succeeded) { var response = fbresult.Object as FBReturnObject; } else { //     } 

This snippet is executed if if (result.Succeeded) returns true (that is, after successful authentication). It uses the popular JSON parser called NewtonSoft.JSON, which needs to be installed from NuGet .

In addition, it is necessary that the following class be present in the project, which characterizes the result of the operation:

  public class FBReturnObject { public string Id { get; set; } public string Post_Id { get; set; } } 

There is an option to place a message in the ribbon, while displaying a dialog box in which the user can add a comment. The code is a bit simpler:

  PropertySet parameters = new PropertySet(); parameters.Add("title", ""); parameters.Add("link", "https://www.microsoft.com/ru-RU/store/p/take-a-test/9wzdncrdkwgx"); parameters.Add("description", "    "); parameters.Add("message", "     "); FBResult fbresult = await session.ShowFeedDialogAsync(parameters); if (fbresult.Succeeded) { var response = fbresult.Object as FBReturnObject; } else { //     } 

Using the following small snippet, a user can send a link to one of his friends and add a message to it:

  PropertySet parameters = new PropertySet(); parameters.Add("link", "http://www.habrahabr.ru"); FBResult fbresult = await session.ShowSendDialogAsync(parameters); 

Examples of applications using winsdkfb are on GitHub , but for C # there is only one example so far and that is not very clear.

More or less normal documentation is under the link
In particular, besides the already considered ways of interacting with fb, the following possibilities are considered: uploading photos / videos, sending invitations to friends (only for games), like some object or link.

Source: https://habr.com/ru/post/307026/


All Articles