Getting FB Page data from facebook using C#

Go To


In my Desktop application, I want to read the Wall posts,Messages, Like counts etc for a particular Facebook page (not for a facebook user)

I went through this post get user data(on stackoverflow). I want to achieve the same thing but for a FB page.

I am ready to create a facebook application to achieve this and have the user to give permission to pull the data.

Please advice on the above.

2012-04-04 07:29
by HishHash


You need an access token to get page data from Facebook. First get an access token using below URL with your facebook application's parameters:{yourappid}&client_secret={yourappscret}

Then you can call the Facebook Graph API with returning token



An example code would be;

class Program
    static void Main(string[] args)
        var client = new WebClient();

        string oauthUrl = string.Format("{0}&client_secret={1}", "appid", "appsecret");

        string accessToken = client.DownloadString(oauthUrl).Split('=')[1];

        string pageInfo = client.DownloadString(string.Format("{0} ", accessToken));
        string pagePosts = client.DownloadString(string.Format("{0} ", accessToken));
2012-04-04 07:58
by Mehmet Osmanoglu
The above really gave me a quick idea on how to get the access token. I would like to go one step further. The above steps would help me pull the info & posts of a page. Now from the software i want the user to reply/comment to a particular post on the wall as the page admin. This is what i want my application to achieve at the end.

Appreciate your reply - HishHash 2012-04-04 08:10


After researching i have developed this code

 class Posts
    public string PostId { get; set; }
    public string PostStory { get; set; }
    public string PostMessage { get; set; }
    public string PostPictureUri { get; set; }
    public Image PostImage { get; set; }
    public string UserId { get; set; }
    public string UserName { get; set; }


    private List<Posts> getFBPosts()
     //Facebook.FacebookClient myfacebook = new Facebook.FacebookClient();
     string AppId = "--------";
     string AppSecret = "----------";
    var client = new WebClient();

    string oauthUrl = string.Format("{0}&client_secret={1}", AppId, AppSecret);

    string accessToken = client.DownloadString(oauthUrl).Split('=')[1];

     FacebookClient myfbclient = new FacebookClient(accessToken);
   string versio= myfbclient.Version;
   var parameters = new Dictionary<string, object>();
   parameters["fields"] = "id,message,picture";
   string myPage="fanPage"; // put your page name
    dynamic result = myfbclient.Get(myPage +"/posts", parameters);

    List<Posts> postsList = new List<Posts>();

    for (int i = 0; i <; i++)
        Posts posts = new Posts();

        posts.PostId =[i].id;
        posts.PostPictureUri =[i].picture;

         var request = WebRequest.Create(posts.PostPictureUri);
        using (var response = request.GetResponse())
        using (var stream = response.GetResponseStream())
                         posts.PostImage  = Bitmap.FromStream(stream);
    return postsList;

2016-09-09 08:16
by Syed Asad Ali Zaidi
Could you explain it a bit more, rather than just pasting it - byxor 2016-09-09 08:36
This code fetches Facebook Fan page posts {id, messeage and picture of each post} and save in a list. It uses Facebook graph API v.2.7. You may find many other codes but many of them are now obsolete. This is working till 13th Sept, 2016. You just need to provide app ID, App Secret and the facebook fan page name you would like to fetch data - Syed Asad Ali Zaidi 2016-09-13 13:15


You can also use a Nuget package called Facebook to fetch data from Facebook graph. Also, Json.NET helps you map the data directly into objects:

public class FacebookPageInfo
    public long Id { get; set; }
    public string Name { get; set; }

public class FacebookPost
    public string Message { get; set; }
    // ReSharper disable once InconsistentNaming
    public string Created_Time { get; set; }
    public string Id { get; set; }

public class FacebookPagingInfo
    public string Previous { get; set; }
    public string Next { get; set; }

public class FacebookPostData
    public List<FacebookPost> Data { get; set; }
    public FacebookPagingInfo Paging { get; set; }

public class Friend
    public string Id { get; set; }
    public string Name { get; set; }

// get access token
string oauthUrl = $"{appId}&client_secret={appSecret}";
string accessToken = client.DownloadString(oauthUrl).Split('=')[1];

// get data and deserialize it
var fbClient = new FacebookClient(accessToken);
var fbData = fbClient.Get("/wikipedia/").ToString();
var info = JsonConvert.DeserializeObject<FacebookPageInfo>(fbData);
fbData = fbClient.Get("/wikipedia/posts").ToString();
var posts = JsonConvert.DeserializeObject<FacebookPostData>(fbData);
2017-02-14 17:25
by Alexei