使用 Windows Facebook SDK 的 FBUser 类会丢失信息

Posted

技术标签:

【中文标题】使用 Windows Facebook SDK 的 FBUser 类会丢失信息【英文标题】:Using the Windows Facebook SDK the FBUser class misses information 【发布时间】:2015-08-06 17:58:50 【问题描述】:

使用Windows SDK for Facebook我已经成功登录并设置了正确的权限来检索一些信息。

这是使用此代码完成的

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

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

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

现在的问题是我只能从 FBUser 中检索一小部分信息,而不是其中存在的所有字段,例如:

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】:

与 ActiveSession 关联的 FBUser 从一开始就只填充了您从初始“登录工作”请求(这是对/me Uri 的标准请求)获得的数据。

这仅包括:

全名 Facebook ID

要获取有关用户的更多数据,您需要使用Graph API,并将您感兴趣的字段作为参数包含在请求中。

这是使用用户的名字、姓氏和语言环境更新当前 FBUser 的一个示例。

// 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;

请注意,示例代码仅包含非常少量的验证。由于这包括网络调用,因此应添加更多内容。

【讨论】:

以上是关于使用 Windows Facebook SDK 的 FBUser 类会丢失信息的主要内容,如果未能解决你的问题,请参考以下文章

使用 Windows Facebook SDK 的 FBUser 类会丢失信息

无法使用 Facebook C# SDK 注销 Facebook

计算facebook sdk需要的key hashes

尝试使用 Graph API (Facebook C# SDK) 更新 Facebook 事件

无法使用 Facebook Android SDK 和 Facebook Audience Network SDK 编译

QuickBlox 使用 Facebook 自己的 SDK 登录?