locked
how to get facebook friends list in asp.net? RRS feed

  • Question

  • User-1078128378 posted

    Hi All,

    I want to get my Facebook friends list in asp.net application.

    I am using Facebook c# sdk.

    I created a Facebook App.

    I am using following code:

    Default2.aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style=" border: 1px solid black; height: 350px; width: auto">
            <br />
           <center>
             <asp:Button ID="Button1" runat="server" Text="Login with facebook" OnClick="Button1_Click" />
            <br />
            <asp:Label ID="lblEmail" runat="server" Text="Email"></asp:Label>
            <br />
            <asp:Label ID="Label2" runat="server" Text="Friends List:"></asp:Label>
            <br />
            <asp:ListBox ID="ListBox1" style="width: 300px; height: 200px;" runat="server"></asp:ListBox>
            <br />
            <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
            <br />
            <asp:Label ID="lblUserId" runat="server" Text="User Id:"></asp:Label>
            
            </center>
        </div>
        </form>
    </body>
    </html>
    

    Default2.aspx.cs:

    using Facebook;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {       
               //check user is already logged in
            if (Session["AccessToken"] != null)
            {
                GetUserData(Session["AccessToken"].ToString());
            } 
            else if (Request.QueryString["code"] != null)
            {
                string accessCode = Request.QueryString["code"].ToString();
                var fb = new FacebookClient();
                // throws OAuthException 
                dynamic result = fb.Post("oauth/access_token", new
                {
                    client_id = "745526535524148",
                    client_secret = "66bede4eea46a0bc4a2222e0b0260a5b",
                    redirect_uri = "http://localhost:60234/Default2.aspx",
                    code = accessCode
                });
                var accessToken = result.access_token;
                var expires = result.expires;
                // Store the access token in the session
                Session["AccessToken"] = accessToken;
                GetUserData(Session["AccessToken"].ToString());
            }
        }
        private void GetUserData(string accessToken)
        {
            var fb = new FacebookClient(accessToken);
    
            dynamic me = fb.Get("me?fields=friends,name,email");
    
            string id = me.id; // Store in database
            string email = me.email; // Store in database
            string FBName = me.name; // Store in database            
            lblEmail.Text = "Email="+email;
            lblUserId.Text = "User Id="+id;
           // NameText.Visible = true;
             lblName.Text = FBName;
    
            ViewState["FBName"] = FBName; // Storing User's Name in ViewState
    
            try
            {
                var friends = me.friends;
    
                foreach (var friend in (JsonArray)friends["data"])
                {
                    ListItem item = new ListItem((string)(((JsonObject)friend)["name"]), (string)(((JsonObject)friend)["id"]));
                    ListBox1.Items.Add(item);
                }
            }
            catch(Exception e)
            {
                ListBox1.Items.Add(e.Message.ToString());
            }              
            Button1.Text = "Log Out";
        }
        private void dologin()
        {
            FacebookClient fb = new FacebookClient();
            var loginUrl = fb.GetLoginUrl(new
            {
                client_id = "745526535524148",
                redirect_uri = "http://localhost:60234/Default2.aspx",
                response_type = "code",
                scope = "email" // Add other permissions as needed
            });
            Response.Redirect(loginUrl.AbsoluteUri);
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
             if (Button1.Text == "Log Out")
                logout();
            else
            {
                dologin();
            }
            
        }
        private void logout()
        {
            var fb = new FacebookClient();
            var logoutUrl = fb.GetLogoutUrl(new
            {
                access_token = Session["AccessToken"],
                next = "http://localhost:60234/Default2.aspx"
    
            });
            // User Logged out, remove access token from session
            Session.Remove("AccessToken");
            Button1.Text = "Log In with Fb"; 
            Response.Redirect(logoutUrl.AbsoluteUri);
        }
    }

    Output:

    i am getting following output:

    https://www.facebook.com/photo.php?fbid=704006743040338&set=a.140484002725951.30333.100002930567133&type=1&theater

    I am getting error when fetching friends list

    i checked in debug mode i am getting null value from json

    what I am missing in code?

    how can i get the friends list?

    and I didnot set any app permission to my app in facebook.

    Thanks,

    Murali.

    Friday, December 19, 2014 2:41 AM

All replies

  • User-1078128378 posted

    Please anyone solve this issue

    Monday, December 22, 2014 1:57 AM
  • User625895049 posted

    same problem with me .Please anyone solve this issue..

    Thursday, April 30, 2015 4:56 AM
  • User-1264905996 posted
     var loginUrl = fb.GetLoginUrl(new
            {
                client_id = "745526535524148",
                redirect_uri = "http://localhost:60234/Default2.aspx",
                response_type = "code",
                scope = "email,user_friends" // Add other permissions as needed
            });

    You must add the user_friends permission in the scope parameter.
    Thursday, July 30, 2015 2:48 AM