如何使用Facebook C#SDK获取用户好友列表?

时间:2014-06-26 22:06:01

标签: c# facebook facebook-c#-sdk

如何使用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

1 个答案:

答案 0 :(得分:0)

首先,你需要上课才能获得Facebook以JSON格式提供的结果中的所有数据。 然后你需要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();
}