Facebook SDK C# - get friends list

前端 未结 2 1119
醉酒成梦
醉酒成梦 2020-12-09 23:47

Using Facebook SDK (http://facebooksdk.codeplex.com/), you do something like this...

        string myAccessToken = \"something\";
        FacebookClient cli         


        
相关标签:
2条回答
  • 2020-12-10 00:08

    Your function for storing friends list from json data:

    string myAccessToken = "something";         
    FacebookClient client = new FacebookClient(myAccessToken);         
    
    var friendListData = client.Get("/me/friends");
    JObject friendListJson = JObject.Parse(friendListData.ToString()); 
    
    List<FbUser> fbUsers = new List<FbUser>();
    foreach (var friend in friendListJson["data"].Children())             
    {   
        FbUser fbUser = new FbUser();
        fbUser.Id = friend["id"].ToString().Replace("\"", "");                 
        fbUser.Name = friend["name"].ToString().Replace("\"", "");                 
        fbUsers.Add(fbUser);
    }
    

    Class for facebook user

    Class FbUser {
        String Id { set; get; }
        String Name { set; get; }
    }
    
    0 讨论(0)
  • 2020-12-10 00:09

    As I had the same issue and was looking a nice solution. I did the same thing with linq resulting in less code. And most of the time less is more :)

    FacebookClient client = new FacebookClient(accessToken);
    dynamic friendListData = client.Get("/me/friends");
    var result = (from i in (IEnumerable<dynamic>)friendListData.data
                 select new
                 {
                     i.name,
                     i.id
                 }).ToList();
    
    0 讨论(0)
提交回复
热议问题