开发者

How to get user's friends, from facebook, and send them all messages, by using C# Facebook SDK?

I'm writing libraty, that will give interfase for geting User's friends, from facebook, and send them all messages, by using C# Facebook SDK('How send messages', I see some topics in here, so this not main for now, I need get User friends).

So I create Application, add Facebook Libreary, Create Application on Facebook and get APP_ID and APP secret.

I try 3-rd day to write somting by using they documentation. That's what I got:

        var fb = new FacebookApp("My_APP_Id", "MySecret");
        var accessToken = fb.Session.AccessToken;

        var app = new FacebookApp(accessToken);
        var result = (JsonObject)app.Get("/HereMyId/frien开发者_如何学JAVAds");
        var model = new List<Friend>();

        foreach (var friend in (JsonArray)result["data"])
            model.Add(new Friend()
                          {
                              Id = (string) (((JsonObject) friend)["id"]),
                              Name = (string) (((JsonObject) friend)["name"])
                          });
        text.Text += "<br/>";
        foreach (Friend res in model)
        {
            text.Text += res.Id + "<br/>";
            text.Text += res.Name + "<br/>";
        }
public class Friend
{
    public string Id { get; set; }
    public string Name { get; set; }
}

I have next error:

Can't lookup all friends of 100002435634. Can only lookup for the logged in user (0), or friends of the logged in user with the appropriate permission.

How I should log in User? Maybe I don't understand how it's should work.

So I have couple questions:

  1. Can I take Login and Pass of user and Connect some how, or maybe I can give some permissions to application, or how it's should work?

  2. How can I get information about friends, from facebook?


Your Facebook application registration must explicitly declare that you require permission to read the friends list. Required permissions are passed in using the scope parameter during authentication.


I know I may be a year late but since I had same problem and made use of your code with chances that worked for your purpose, I returned with working code for those that may come here with same problem. Here is the edited code that was originally yours.

public void ShowFriendList(string at, string id)
{
    //var fb = new FacebookApp("My_APP_Id", "MySecret");
    var accessToken = at;

    Facebook.FacebookClient app = new Facebook.FacebookClient(accessToken);
    var result = (Facebook.JsonObject)app.Get("/" + id + "/friends");
    var model = new List<fbUser>();  //model = friendlist array
    int i = 5; //limit to five friends

    foreach (var friend in (Facebook.JsonArray)result["data"])
    {
        if (i != 0)
        {
            model.Add(new fbUser()
            {
                ID = (string)(((Facebook.JsonObject)friend)["id"]),
                Name = (string)(((Facebook.JsonObject)friend)["name"])


            });
            i = i - 1;
        }
        else
        {
            break;
        }
    }
    lblreport.Text += "<br/>";
    foreach (fbUser res in model)
    {
        lblreport.Text += res.ID + "<br/>";
        lblreport.Text += res.Name + "<br/>";
    }
   }

I only replaced text output control and Friend class with lblreport label and fbUser class respectively, just replace them back if you like.

The only thing you missed is you used the application access_token instead of the logged in user's access_token as you planned to do. Otherwise your code was an excellent example in pulling out friends list from a Facebook Json Object which is what I was after in the first place. I hope this helps. PS: this is my first time answering on stackoverflow so I hope I am contributing to this great resource site.


var client = new FacebookClient(access_token);
var result = client.Get("/me/friends?fields=picture,name") as JsonObject;
JavaScriptSerializer js = new JavaScriptSerializer();
string str = result ["data"].ToString();
List<Friends> objFriends = (List<Friends>)js.Deserialize(str, typeof(List<Friends>));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜