【问题标题】:Getting FB Page data from facebook using C#使用 C# 从 facebook 获取 FB Page 数据
【发布时间】:2017-02-15 01:25:51
【问题描述】:

在我的桌面应用程序中,我想阅读特定 Facebook 页面 (不适用于 facebook 用户)的 Wall 帖子、消息、喜欢计数等

我浏览了这篇文章get user data(on stackoverflow)。除了 FB 页面,我想实现同样的目标。

我准备创建一个 facebook 应用程序来实现这一点,并让用户授予提取数据的权限。

请对以上内容提出建议。

【问题讨论】:

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


    【解决方案1】:

    您需要访问令牌才能从 Facebook 获取页面数据。 首先使用以下 URL 和您的 facebook 应用程序参数获取访问令牌:

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

    然后您可以调用 Facebook Graph API 并返回令牌

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

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

    示例代码是;

    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));
        }
    }
    

    【讨论】:

    • 以上内容确实让我快速了解了如何获取访问令牌。我想更进一步。上述步骤将帮助我提取页面的信息和帖子。现在从软件中,我希望用户以页面管理员的身份回复/评论墙上的特定帖子。这就是我希望我的应用程序最终实现的目标。感谢您的回复。
    【解决方案2】:

    经过研究,我开发了这段代码

     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;
    
    }
    

    【讨论】:

    • 你能不能多解释一下,而不是直接粘贴?
    • 此代码获取 Facebook 粉丝页面帖子 {id, message and picture of each post} 并保存在列表中。它使用 Facebook 图形 API v.2.7。您可能会发现许多其他代码,但其中许多现在已过时。有效期至 2016 年 9 月 13 日。您只需要提供应用 ID、应用秘密和要获取数据的 facebook 粉丝页面名称。
    【解决方案3】:

    您还可以使用名为 Facebook 的 Nuget 包从 Facebook 图表中获取数据。此外,Json.NET 可以帮助您将数据直接映射到对象中:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多