2012-03-06 72 views

回答

0

下面是C#中Facebook API的簡單示例。它顯示了獲取職位特定搜索條件:

public class Post 
    { 
     private string id; 

     public string Id 
     { 
      get { return id; } 
      set { id = value; } 
     } 
     private string created_time; 

     public string Created_time 
     { 
      get { return created_time; } 
      set { created_time = value; } 
     } 
     private string message; 

     public string Message 
     { 
      get { return message; } 
      set { message = value; } 
     } 
    } 

public class FacebookHelpers 
    { 
     private const string PUBLIC_POSTS = "/search"; 
     public List<Post> SearchPosts(Dictionary<string, string> parameters) 
     { 
      List<Post> postsList = new List<Post>(); 
      try 
      { 
       var app = new FacebookAPI(); 
       dynamic result = app.Get(PUBLIC_POSTS, parameters); 


       foreach (dynamic item in result.Dictionary["data"].Array) 
       { 

        if (item.Dictionary.ContainsKey("message")) 
        { 

         List<JSONObject> valueList = new List<JSONObject>(item.Dictionary.Values); 

         Post p = new Post(); 
         p.Message = valueList[0].String; 
         p.Id = valueList[1].String; 
         p.Created_time = valueList[2].String; 
         postsList.Add(p); 

        } 
       } 
       return postsList; 

      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine("Error: " + ex.Message); 
       return postsList; 
      } 


     } 

public List<Post> SearchPosts(string searchTerm, string until = "today", uint maxRecords = 100) 
     { 
      Dictionary<string, string> parameters = new Dictionary<string, string>(); 
      parameters.Add("q", searchTerm); 
      parameters.Add("type", "post"); 
      parameters.Add("until", until); 
      parameters.Add("fields", "message"); 
      parameters.Add("limit", Convert.ToString(maxRecords)); 

      return SearchPosts(parameters); 
     } 
    } 

您將需要FacebookAPI.dll和NewtonSoftJson.dll。我希望這對你有一些用處。歡呼:)

+0

謝謝,我已經使用Facebook C#sdk,我想這是另一個。 FacebookAPI.dll是新的嗎?有人在開發,更新嗎? :) – 2012-03-06 14:39:08

相關問題