有没有一种简单的方法来使用Facebook c#sdk?

时间:2012-03-06 12:45:07

标签: facebook-c#-sdk

我正在使用Facebook C#sdk寻找启动示例。我找到了sdk和很多不同的样本,但写的是mvc。我希望在web.config中找到使用aspx文件,配置的东西,当然还有使用sdk的dll:s。我想检索信息并发布信息。

1 个答案:

答案 0 :(得分: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。我希望这对你有用。干杯:)