.Net Facebook Developer Toolkit – 讀取自己 Facebook Account 既 Friend List

 
.Net Facebook Developer Toolkit – 讀取自己 Facebook Account 既 Friend List
 
Reference 要 Import 上 "C:\Program Files\Coding4Fun\Facebook\Binaries" 既 "Facebook.WebControls.dll" / "Facebook.dll"
 

   WebForm6.aspx
 
   … …
 
   <asp:Literal ID="Literal2" runat="server"></asp:Literal>
 
   … …
 

 

   WebForm6.aspx.cs
 
   using System;
   using Facebook;
   using Facebook.WebControls;
 
   namespace WebApplication12
   {
 
      public partial class WebForm6 : System.Web.UI.Page
      {
         Facebook.Components.FacebookService FBService = new Facebook.Components.FacebookService();
 
         protected void Page_Load(object sender, EventArgs e)
         {
            CheckLogin();
 
            if (!IsPostBack){
               GetFriendList();
            }
         }
 
         private void GetFriendList()
         {
            if (FBService.GetFriends() != null && FBService.GetFriends().Count > 0){
               int i = 0;
 
               Literal2.Text += "<table border='0' cellpadding='3' cellspacing='0'>";
 
               foreach (Facebook.User user in FBService.GetFriends()){
                  if ((i % 6) == 0){
                     Literal2.Text += "<tr>\n";
                  }
 
                  Literal2.Text += "<td align='center'>\n";
                  Literal2.Text += "<a href='http://www.facebook.com/" + user.UserId.Trim() + "' style='outline:none;'>\n";
                  Literal2.Text += "<img border='0' src='" + user.PictureUrl.ToString() + "'><br />\n";
                  Literal2.Text += user.Name.ToString().Trim() + "\n";
                  Literal2.Text += "</a></td>\n";
 
                  if ((i % 6) == 5){
                     Literal2.Text += "</tr>\n";
                  }
 
                  i++;
               }
 
               Literal2.Text += "</table>";
            }else{
               Literal2.Text = "null<br/>";
            }
         }
 
         private void CheckLogin()
         {
            FBService.ApplicationKey = "<應用程式 ID/API 鑰匙>";
            FBService.Secret = "<應用程式密鑰>";
 
            FBService.IsDesktopApplication = false;
 
            string sessionKey = Session["Facebook_session_key"] as String;
            string userId = Session["Facebook_userId"] as String;
 
            string authToken = Request.QueryString["auth_token"];
 
            if (!String.IsNullOrEmpty(sessionKey)){
               FBService.SessionKey = sessionKey;
               FBService.UserId = userId;
            }else if (!String.IsNullOrEmpty(authToken)){
               FBService.CreateSession(authToken);
               Session["Facebook_session_key"] = FBService.SessionKey;
               Session["Facebook_userId"] = FBService.UserId;
               Session["Facebook_session_expires"] = FBService.SessionExpires;
            }else{
               Response.Redirect(@"http://www.Facebook.com/login.php?api_key=" + FBService.ApplicationKey + @"&v=1.0");
            }
         }
      }
   }