How to get Number of shares and Likes for a URL using FacebookGraph API

末鹿安然 提交于 2019-12-13 07:25:00

问题


How to get Number of shares and Likes for a URL using Facebook Graph API? There are a few posts answering a similar question but every post offers a different method/fb API.

I am using the C# SDK and I guess I should be using the Graph API since FQL is not supported in the latest FB API.

This answer looks nice but the poster said the returned value for shares is sum of shares and likes for this URL and I need them separately.


回答1:


After Posting post into facebook you will get Post ID(138885734110127_1484064888656364) in return from facebook. Using that post id you can get counts of likes and comments and who likes and comments, and what comment posted. I don't know about shares.

For getting likes and comments here is the code:

            var fb = new FacebookClient("Your Access token here");
            var WallPost = fb.Get("138885734110127_1484064888656364");


            JObject jObj = JObject.Parse(WallPost.ToString());
            var Comments = jObj.Property("comments");
            var Likes = jObj.Property("likes");

You will get in json. Hope this helps. :)




回答2:


I think I maybe rather late, but here is my code for getting shares,likes and comments for URL, please observe that it doesn't require an access token, hope this helps in some way!.

string url = "http://www.youtube.com";     
string QUrl = "https://graph.facebook.com/?fields=id,share,og_object{engagement{count},likes.summary(true).limit(0),comments.limit(0).summary(true)}&id=" + url;

        System.Net.HttpWebRequest Request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(QUrl);
        Request.ContentType = "text/json";     
        Request.Timeout = 10000;
        Request.Method = "GET"; 
        string content;


        using (WebResponse myResponse = Request.GetResponse())
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8))
            {
                content = sr.ReadToEnd();
            }
        };

        var json = JObject.Parse(content);

        var like_count = json["og_object"]["likes"]["summary"]["total_count"];

        Console.WriteLine("Like Count :" + like_count);

        var share_count = json["share"]["share_count"];

        Console.WriteLine("Share Count :" + share_count);

        var comment_count = json["og_object"]["comments"]["summary"]["total_count"];

        Console.WriteLine("Comment Count :" + comment_count);


来源:https://stackoverflow.com/questions/32701732/how-to-get-number-of-shares-and-likes-for-a-url-using-facebookgraph-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!