Getting FB Page data from facebook using C#

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:47:56

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:

https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={yourappid}&client_secret={yourappscret}

Then you can call the Facebook Graph API with returning token

General: https://graph.facebook.com/wikipedia?access_token={token}

Posts: https://graph.facebook.com/wikipedia/posts?access_token={token}

An example code would be;

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

        string oauthUrl = string.Format("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={0}&client_secret={1}", "appid", "appsecret");

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

        string pageInfo = client.DownloadString(string.Format("https://graph.facebook.com/wikipedia?access_token={0} ", accessToken));
        string pagePosts = client.DownloadString(string.Format("https://graph.facebook.com/wikipedia/posts?access_token={0} ", accessToken));
    }
}

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("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={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>();
    int mycount=result.data.Count;

    for (int i = 0; i < result.data.Count; i++)
    {
        Posts posts = new Posts();

        posts.PostId = result.data[i].id;
        posts.PostPictureUri = result.data[i].picture;
        posts.PostMessage= result.data[i].message;

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

}

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 = $"https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!