Facebook Graph API分页如何工作,以及如何使用它来迭代Facebook用户feed? [英] How does Facebook Graph API Pagination works and how to iterate facebook user feed with it?

查看:147
本文介绍了Facebook Graph API分页如何工作,以及如何使用它来迭代Facebook用户feed?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个facebook Graph API调用来获取一个facebook用户供稿:

I have a facebook Graph API call to get a facebook users feed:

dynamic myFeed = await fb.GetTaskAsync(
                    ("me/feed?fields=id,from {{id, name, picture{{url}} }},story,picture,link,name,description," +
                    "message,type,created_time,likes,comments")
                    .GraphAPICall(appsecret_proof));

以上内容会返回一段时间内的一些最新用户帖子,例如21或22个帖子,但不会返回用户帖子的完整列表. 我搜索了一种使用Facebook分页来迭代用户Feed的方法,最终找到了可与Facebook Offset分页一起使用的解决方案.

The above returns a number of the latest user posts in a while say 21 or maybe 22 posts but not the complete list of user posts. I searched for a way to iterate through a users feed using facebook pagination and I ended up finding this solution which works with facebook Offset pagination.

dynamic myFeed = await fb.GetTaskAsync(
                    ("me/feed?fields=id,from {{id, name, picture{{url}} }},story,picture,link,name,description," +
                    "message,type,created_time,likes,comments")
                    .GraphAPICall(appsecret_proof), new {limit = "1000", offset = "21" });

这使我更接近实现目标,但我认为这不是理想的实现方式,并且它不会返回所有用户的帖子. 有什么解决方法吗?请帮忙.

This has taken me a step nearer to what I want to achieve but I think this is not the ideal way to do it and also it does not return all of users posts. Is there any workaround? Please help.

P.S:我正在使用Facebook C#SDK.

P.S: I am using Facebook C# SDK.

UPDATE1: 按照杰里米的回答.看来,facebook光标分页是满足我要求的唯一正确选择.我想知道C#facebook sdk是否提供了在Next Edge上进行迭代的任何功能,这样我就可以在一次调用中获得所有供稿信息,对此是否有可能的解决方案? PS:我已经浏览过Facebook API文档很多次了,我知道节点,边缘和字段的确切含义,唯一不幸的是,Facebook还不支持C#SDK,并且我无法找到有关C#SDK的文档. Facebook C#SDK也是如此.

UPDATE1: As per Jeremy's answer. It seems that the facebook cursor pagination is the only right option for my requirements. I would like to know if C# facebook sdk provides any feature to iterate over the Next Edges so I can get all feed posts in one call, Is there any possible solution to this? P.S: I've been through facebook API docs many times and I know what exactly the Nodes, Edges and Fields are, the only unfortunate thing is that facebook doesn't support C# SDK as yet and I was unable to find a proper documentation on Facebook C# SDK too.

推荐答案

最后,在做了一些研究并阅读了一些博客之后,我发现没有直接来自Facebook的直接API CAlls可以一次提取所有用户供稿者帖子. 要实现该功能,无论Jeremy Thomson建议如何进行无限滚动,或者遍历不同的Facebook数据页面,无论edge支持哪种facebook pagination类型.就我想要一个没有用户干预/动作的过程而言,我肯定会选择第二个选择,它使用while循环遍历Facebook数据页面. 为此,我们首先需要两个最重要的参数(facebook access_token +(facebook appsecret_proof),如下所述:

Finally after doing some researches and reading some blogs I found out that there is no direct API CAlls from facebook to fetch all user feeder posts in once. To achieve that functionality either one has to go for infinite scrolling as suggested by Jeremy Thomson or to iterate through different facebook data pages regardless of which facebook pagination type is supported by the edge. As far as I want a process without user interference/actions I would definitely go the second option which is iterating through facebook data pages with a while loop. To do That we first need our two most important parameters (facebook access_token + (facebook appsecret_proof) as described below:

var appsecret_proof = access_token.GenerateAppSecretProof();
var fb = new FacebookClient(access_token);

要记住的要点: facebook access_token是由HttpContext类生成的.

Point to be remembered: facebook access_token is generated by HttpContext class.

facebook API调用将获得用户的前25条馈送帖子,如下所示:

The facebook API call will get the users first 25 feeder post as below:

dynamic myFeed = await fb.GetTaskAsync(
                    ("me/feed?fields=id,from {{id, name, picture{{url}} }},story,picture,link,name,description," +
                    "message,type,created_time,likes,comments")
                    .GraphAPICall(appsecret_proof));

上面的API调用返回一个Json数组,该数组应通过Model View属性进行水合,如下所示:

The API Call above return results in a Json array and that should be hydrated through the Model View properties as shown here:

var postList = new List<FacebookPostViewModel>();
    foreach (dynamic post in myFeed.data)
       {
         postList.Add(DynamicExtension.ToStatic<FacebookPostViewModel>(post));
       }

在此之前,所有内容都已明确,最重要的部分肯定是在获取所有Facebook用户帖子.为此,我们需要将string NextPageUri设置为空,如下所示:

Until here everything was clear before, the most important part which is surely fetching all facebook user post is now in action. For that we need to set a string NextPageUri to empty as here:

string NextPageURI = string.Empty;

最后一部分是检查数据是否还有另一页,如果是,则应进行迭代并将数据添加到View Model,直到没有页面提升为止,如下所示:

The final part of all is to check if there is another page for the data, If yes should iterate and add the data to the View Model until there is no page lift as shown here:

while (myFeed.paging != null && myFeed.paging.next != null)
                {
                    NextPageURI = myFeed.paging.next;
                    var nextURL = GetNextPageQuery(NextPageURI, access_token);
                    dynamic nextPagedResult = await fb.GetTaskAsync(nextURL.GraphAPICall(appsecret_proof));
                    foreach (dynamic post in nextPagedResult.data)
                    {
                        postList.Add(DynamicExtension.ToStatic<FacebookPostViewModel>(post));
                    }
                }

这帮助我摆脱了所面临的问题.但是,我还有另一项工作要做.如果帖子超过3万个,则获取帖子的速度会花费10分钟,这至少对我来说并不理想.

This helped me to get rid of the problem faced. But yet I have another task in hand to work on. It is the speed of fetching the posts that if posts are morethan 30k would take 10 minutes which is not ideal at least for me.

这篇关于Facebook Graph API分页如何工作,以及如何使用它来迭代Facebook用户feed?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆