且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过FB API从个人资料中检索Facebook帖子

更新时间:2023-12-02 18:12:28

先决条件:-您需要为所有请求提供有效的FB令牌:- 我在回答使用C#语言. 步骤1:-首先,您需要使用FB令牌获取用户的FB ID

Prerequisite:- You need to have valida FB token for all the request:- I am answering using c# language. Step 1:- First you need to get the user's FB id using FB token

我正在使用Facebook SDK 7.0.6进行查询

I am using Facebook SDK 7.0.6 for querying purpose

这是初始化在连续调用中使用的FB服务的方法.

here's how to initialize the FB service which we will use in our consecutive calls.

FacebookService facebookService = new FacebookService(facebookToken);
var _facebookClient = new Facebook.FacebookClient(token);

代码段

public string GetFacebookID(string facebookToken)
        {
            dynamic result = _facebookClient.Get("me?fields=id");

            if (result.ToString().Contains("id"))
            {
                return result["id"];
            }
            return string.Empty;
        }

之后,您可以执行以下方法,使用FB ID和令牌获取用户的帖子

after that you can execute below method to get user's post using FB ID and token

public List<Post> GetPostsForUser(string facebookID)
        {
            List<Post> posts = new List<Post>();

            dynamic result = _facebookClient.Get(facebookID + "/posts"); //Case Sensitive, Posts doesn´t work

            if (result.ToString().Contains("data") && result.data.Count > 0)
            {

                foreach (var item in result.data)
                {
                    posts.Add(new Post
                    {
                        ID = item.id,
                        Story = item.story,
                        Message = item.message,
                        Created_Time = Convert.ToDateTime(item.created_time),
                        Reactions = GetReactions(item.id)
                    });
                }

                result = _facebookClient.Get(GetNextURL(result.ToString()));

            }
            return posts;
        }