获取我和我朋友的帖子(Wall Facebook)

时间:2015-05-10 09:28:30

标签: c# facebook facebook-graph-api facebook-sdk-4.0

我用 https://developers.facebook.com/tools/access_token/

我创建了一个App。我希望得到我和朋友们的帖子。

我尝试me/feedme/posts,但我一无所获。

我有这个单元测试。

有什么建议吗?

    [TestMethod]
    public void Basic_using_SDK()
    {
        // http://blog.prabir.me/posts/facebook-csharp-sdk-making-requests
        var fb = new Facebook.FacebookClient();

        var result = (IDictionary<string, object>)fb.Get("4");

        var id = (string)result["id"];
        var name = (string)result["name"];
        var firstName = (string)result["first_name"];
        var lastName = (string)result["last_name"];
        var link = (string)result["link"];
        var username = (string)result["username"];
        var gender = (string)result["gender"];
        var male = (string)result["locale"];


        var parameters = new Dictionary<string, object>();
        parameters["fields"] = "id,name";

        result = (IDictionary<string, object>)fb.Get("4", parameters);
        id = (string)result["id"];
        name = (string)result["name"];


        dynamic result2 = fb.Get("4");

        id = result2.id;
        name = result2.name;
        firstName = result2.first_name;
        lastName = result2.last_name;
        link = result2.link;
        username = result2.username;
        gender = result2.gender;
        male = result2.locale;


        dynamic parameters2 = new ExpandoObject();
        parameters2.fields = "id,name";

        dynamic result3 = fb.Get("4", parameters);
        id = result3.id;
        name = result3.name;

        dynamic me = fb.Get("zuck");
        firstName = me.first_name;
        lastName = me.last_name;


        var client = new FacebookClient(AccessToken);
        dynamic me2 = client.Get("me");
        string aboutMe = me2.about;

        dynamic result4 = client.Get("/me/feed");

        foreach (dynamic post in result4.data)
        {
            var fromName = post.from.name;
            Console.WriteLine(fromName);
        }

        dynamic result5 = client.Get("/me/posts");
        for (int i = 0; i < result5.Count; i++)
        {
        }


        // https://www.facebook.com/profile.php?id=xxxxxxx
        // https://graph.facebook.com/xxxxxxx
        var uidKiquenet = "xxxxx";
        var query = string.Format(@"SELECT status_id,message,time,source,uid,place_id
                            FROM status WHERE uid IN (SELECT uid FROM status WHERE uid = '" + uidKiquenet + "') ORDER BY time DESC");

        dynamic parameters6 = new ExpandoObject();
        parameters6.q = query;

        dynamic results6 = fb.Get("/xxxxx?fields=id,name,age_range,about,email,first_name,gender");


        string myMessage = "Hello from Test";

        fb.PostTaskAsync("me/feed", new { message = myMessage }).ContinueWith(t =>
        {
            if (!t.IsFaulted)
            {
                string message = "Great, your message has been posted to you wall!";
                Console.WriteLine(message);
            }
        });

        fb.GetTaskAsync("me").ContinueWith(t =>
        {
            if (!t.IsFaulted)
            {
                var result11 = (IDictionary<string, object>)t.Result;
                string myDetails = string.Format("Your name is: {0} {1} and your Facebook profile Url is: {3}",
                                                  (string)result11["first_name"], (string)result11["last_name"],
                                                  (string)result11["link"]);
                Console.WriteLine(myDetails);
            }
        });

        // This uses Facebook Query Language
        // See https://developers.facebook.com/docs/technical-guides/fql/ for more information.
        query = string.Format("SELECT uid,name,pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1={0}) ORDER BY name ASC", "me()");

        fb.GetTaskAsync("fql", new { q = query }).ContinueWith(t =>
        {
            if (!t.IsFaulted)
            {
                var result1 = (IDictionary<string, object>)t.Result;
                var data = (IList<object>)result1["data"];
                var count = data.Count;
                var message = string.Format("You have {0} friends", count);
                Console.WriteLine(message);

                foreach (IDictionary<string, object> friend in data)
                    Console.WriteLine((string)friend["name"]);
            }
        });

    }

2 个答案:

答案 0 :(得分:1)

您必须使用正确的权限进行授权才能访问Feed帖子。尝试使用user_posts进行授权,如文档中所述:https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed#read

请注意,这不会让您访问朋友的帖子,除非他们张贴在授权用户的墙上。

关于评论,您可能需要阅读:https://developers.facebook.com/docs/facebook-login/review/faqs#what_is_review

顺便说一下,FQL已弃用,只适用于v2.0应用程序 - 而不是新版本。

答案 1 :(得分:1)

同样的事发生在我身上。这是我的解决方案

我在Facebook开发者控制台上注册了我的项目。然后我做了与你相同的代码,但无法获得帖子/信息。过了一段时间后,我向Facebook团队提交了应用程序审核申请(它仍处于早期开发阶段),经过反馈,我的代码正常运行。因此,等待申请审核也可能是您的问题。此选项在应用程序设置中可用。试一试

<强>更新 另一个代码示例在这里

 if (Request["code"] == null)
            {
                Response.Redirect(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}",
                    FB_app_id, Request.Url.AbsoluteUri, FB_scope));
                return false;
            }
            else
            {
                Dictionary<string, string> tokens = new Dictionary<string, string>();
                string url = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}&perms=status_update"
                    , FB_app_id, Request.Url.AbsoluteUri, FB_scope, Request["code"].ToString(), FB_secret_id);
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {

                    StreamReader rd = new StreamReader(response.GetResponseStream());

                    string vals = rd.ReadToEnd();
                    foreach (string token in vals.Split('&'))
                    {
                        tokens.Add(token.Substring(0, token.IndexOf('=')), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf('=') - 1));
                    }
                    string access_token = tokens["access_token"];
                    Session["fb_access_token"] = access_token;

                    return true;


                }

            }
相关问题