如何使用Facebook c# SDK获取用户好友列表
本文关键字:用户 好友 列表 获取 SDK 何使用 Facebook | 更新日期: 2023-09-27 18:10:23
如何使用facebook c# sdk检索用户好友列表?我需要知道哪些权限允许我得到这些信息。下面是我自己的尝试,但是没有成功:
//retrieve all user friends ids & names in 2D array
public static string[,] getFriends(string accessToken)
{
string[,] friends;
FacebookClient fb = new FacebookClient(accessToken);
dynamic friend = fb.Get("me/friends");
int count = (int)friend.data.Count;
friends = new string[count, 2];
for (int i = 0; i <count; i++)
{
friends[i, 0] = friend.data[i].id;
friends[i, 1] = friend.data[i].name;
}//end for
return friends;
}//end get friends
首先,您需要类从JSON格式的Facebook给出的结果中获取所有数据。然后需要gridview来显示来自类的数据。
下面是获取好友列表的代码:public class MyFriends
{
public long uid { get; set; }
public string username { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public long? friend_count { get; set; } //nullable
public string pic_big { get; set; }
public string birthday { get; set; }
public string birthday_date { get; set; }
public string contact_email { get; set; }
}
protected void friendsList()
{
var fb = new FacebookClient("YOUR TOKEN HERE");
var query = string.Format(@"SELECT uid, username, first_name, last_name, friend_count, pic_big, birthday, birthday_date, contact_email
FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");
dynamic parameters = new ExpandoObject();
parameters.q = query;
dynamic results = fb.Get("/fql", parameters);
List<MyFriends> q = JsonConvert.DeserializeObject<List<MyFriends>>(results.data.ToString());
GridView1.DataSource = q;
GridView1.DataBind();
}