Getting friends from outside website

Jul 6, 2009 at 10:43 PM

Hi,

I would like to add a feature whereby on our website we can show a list of a users's Facebook friends.  Using the desktop example for FDT, I used this:

 

 

            facebookService1.ApplicationKey = api;
            facebookService1.Secret = secret;
            facebookService1.ConnectToFacebook();
            
    var friends = facebookService1.friends.getUserObjects();

 

Works great (as long as I use the api key/secret provided by the example and not my own).  At the connectToFacebook() line some magic happens whereby a new form is launched you can log in at facebook.

 

My question is, how do I do the same thing in a .NET web application/site (ie .aspx page) that is not a facebook application residing in an iframe but is a website in its own right? Calling  facebookService1.ConnectToFacebook() in a web abblication give some kind of threading exception most likely because that call was intended for desktop use only.

Thanks!

 

 

 

 

Jul 7, 2009 at 1:39 AM

nevermind.  I finally got it.  Here is is for anyone that wants it:

 

 

    facebook.Components.FacebookService _fbService = new facebook.Components.FacebookService();
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadFriends();
    }
    private void LoadFriends()
    {
        try
        {
            string KEY = "your key";
            string SECRET = "your secret";
            _fbService.ApplicationKey = KEY;
            _fbService.Secret = SECRET;
            _fbService.IsDesktopApplication = false;
            string sessionKey = "";
            string userId = "";
            if (Session["facebook_session_key"] != null)
            {
                sessionKey = Session["facebook_session_key"].ToString();
            }
            if (Session["facebook_userId"] != null)
            {
                userId = Session["facebook_userId"].ToString();
            }
            // When the user uses the facebook login page,
            // the redirect back here will will have the auth_token in the query params
            string authToken = Request.QueryString["auth_token"];
            if (!String.IsNullOrEmpty(sessionKey))
            {
                _fbService.SessionKey = sessionKey;
                _fbService.uid = long.Parse(userId);
            }
            else if (!String.IsNullOrEmpty(authToken))
            {
                _fbService.CreateSession(authToken);
                Session["facebook_session_key"] = _fbService.SessionKey;
                Session["facebook_userId"] = _fbService.uid.ToString();
                Session["facebook_session_expires"] = _fbService.SessionExpires;
            }
            else
            {
                Response.Redirect(@"http://www.facebook.com/login.php?api_key=" +
                    _fbService.ApplicationKey + @"&v=1.0");
            }
            if (!IsPostBack)
            {
                var friends = _fbService.friends.getUserObjects();
                foreach (facebook.Schema.user friend in friends)
                {
                    ListItem li = new ListItem(friend.name,friend.uid.ToString());
                    lstFacebookFriends.Items.Add(li);
                }
            }
    }