Using the Windows Facebook SDK the FBUser class misses information

186 views Asked by At

Using the Windows SDK for Facebook I've managed to log in and set the correct permissions to retrieve some info.

This was done using this code

FBSession sess = FBSession.ActiveSession;
sess.FBAppId = FBAppId;
sess.WinAppId = WinAppId;

// Add permissions
sess.AddPermission("public_profile");

// Do the login
await sess.LoginAsync();

Now the problem is that I can only retrieve a small bit of information from the FBUser and not all the fields that exists within it, for example:

sess.User.Name; // This WORKS
sess.User.Id; // This WORKS
sess.User.FirstName; // DOESN'T WORK
sess.User.LastName; // DOESN'T WORK
sess.User.Locale; // DOESN'T WORK
...
1

There are 1 answers

0
ILOABN On BEST ANSWER

The FBUser associated with the ActiveSession is from the beginning ONLY populated with the data that you get from the initial "did login work"-request (which is a standard request to the /me Uri).

This only includes:

  • Full Name
  • Facebook ID

To get more data about the user you need to make use of the Graph API, and include the fields you're interested in as a parameter in the request.

This is one example how it could look like to update the current FBUser with the first_name, last_name and locale of the user.

// This is the current user that we're going to add info to
var currentUser = FBSession.ActiveSession.User;

// Specify that we want the First Name, Last Name and Locale that is connected to the user
PropertySet parameters = new PropertySet();
parameters.Add("fields", "locale,first_name,last_name");

// Set Graph api path to get data about this user
string path = "/me/";

// Create the request to send to the Graph API, also specify the format we're expecting (In this case a json that can be parsed into FBUser)
FBSingleValue sval = new FBSingleValue(path, parameters,
  new FBJsonClassFactory(FBUser.FromJson));

// Do the actual request
FBResult fbresult = await sval.Get();

if (fbresult.Succeeded)
{
  // Extract the FBUser with new data
  var extendedUser = ((FBUser)fbresult.Object);

  // Attach the newly fetched data to the current user
  currentUser.FirstName = extendedUser.FirstName;
  currentUser.LastName = extendedUser.LastName;
  currentUser.Locale= extendedUser.Locale;
}

Please note that the sample code only includes a very minimum amount of verification. Since this includes network calls more should be added.