我如何使用facebook api循环下一页25结果?

时间:2015-12-28 12:31:58

标签: c# .net facebook winforms facebook-graph-api

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Facebook;
using System.Net;
using System.IO;

namespace WebSite_Login_And_Browsing
{
    class Posts
    {
        public string PostId { get; set; }
        public string PostStory { get; set; }
        public string PostMessage { get; set; }
        public string PostPicture { get; set; }
        public string UserId { get; set; }
        public string UserName { get; set; }

    }

    class FacebookPosts
    {
        static string accesstoken;
        //static string token = "2f89d691b5f39";
        static string token = "1186840401345424|GoJRCpM";
        static string mytoken = "CAACEdEose0cBACPu39NSSalHCGFGDGRKZAvwiTuzG8PHlNRJwbyMVugovDxgL7CT3a1QbRuVDZALXxWU0ntwSrDyq75LIIuzFpBtx47cJYCY2OiA21lpTRKt2bB0t5HrsQYIXHXhmU7GnavWZCzqN8yeuv5NWXxTIOfVCZAZArjYNiPWhZBqZAZAO03s6FKNIulm4kjzXvp4QKiahAlcyaZBg";
        static string mytokenaslip = "CAACEdEose0cBABmWuBI9p9dpPxEsMJoFZAG3kScx61kZAImNBgt52kVrd8WWPRpwjWP8nCPX69zdLuFyVQHzxYfMk85ZBZC4BIajVWXNLo7OI7yaCbNIwqkcdwpabQVFZBRWt0rzTQrQr6ZBij45XnrQyEUqFKP4gADeO4Fl9yRaZAZCOFtV3b84sWUFEgwaKbZAPY4BCljVjWQZDZD";
        public static void RetrievePosts()
        {
            try
            {
                var client = new FacebookClient(mytokenaslip);
                dynamic result = client.Get("/me/posts");
                List<Posts> postsList = new List<Posts>();

                //all the posts and their information (like pictures and links) is strored in result.data not in result

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

                    posts.PostId = result.data[i].id;
                    if (object.ReferenceEquals(result.data[i].story, null))
                        posts.PostStory = "this story is null";
                    else
                        posts.PostStory = result.data[i].story;
                    if (object.ReferenceEquals(result.data[i].message, null))
                        posts.PostMessage = "this message is null";
                    else
                        posts.PostMessage = result.data[i].message;

                    posts.PostPicture = result.data[i].picture;
                    posts.UserId = result.data[i].from.id;
                    posts.UserName = result.data[i].from.name;

                    postsList.Add(posts);
                }

            }
            catch (Exception err)
            {
                //throw;
                string myerr = err.ToString();
            }
        }
    }
}

我在List postsList中获得了25个结果 我如何循环现在asgain以获得下一个25个结果的下一页并将其添加到postsList并反复循环,直到没有更多的结果?

我想要做的是每隔50分钟自动删除最近25个帖子。 在我的项目的其他课程中,我每分钟都会自动发布一个帖子。 50分钟后,我想删除最近的25个帖子,所以我的墙上只会有25个帖子。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
using HtmlAgilityPack;
using System.Net;
using System.IO;

namespace WebSite_Login_And_Browsing
{
    public partial class Facebook_Post : Form
    {
        WebBrowser wb = new WebBrowser();
        int postsCounter = 0;
        StreamWriter w = new StreamWriter(@"e:\posts.txt");
        WebBrowser webBrowser1;

        public Facebook_Post()
        {
            InitializeComponent();

            webBrowser1 = new WebBrowser();
            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
            webBrowser1.ScriptErrorsSuppressed = true;
            webBrowser1.Navigate("https://www.facebook.com/");
            label4.Text = DateTime.Now.ToString();
            w.WriteLine(label4.Text.ToString());
            w.WriteLine(Environment.NewLine);
            label5.Visible = false;
            label2.Visible = false;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                if (e.Url.AbsoluteUri != webBrowser1.Url.AbsoluteUri)
                {
                    return;
                }


                wb = webBrowser1;

                foreach (HtmlElement he in wb.Document.All.GetElementsByName("xhpc_message"))
                {
                    he.SetAttribute("value", RandomString(10));
                }
                var elems = wb.Document.GetElementsByTagName("button");

                foreach (HtmlElement elem in elems)
                {
                    if (elem.InnerText == "Post")
                    {
                        elem.InvokeMember("click");
                    }
                }
                sent = true;
                postsCounter += 1;
                label2.Text = postsCounter.ToString();
                label2.Visible = true;
                timer1.Enabled = true;
                webBrowser1.Dispose();
                if (postsCounter == 720)
                {
                    w.WriteLine(postsCounter.ToString());
                    w.WriteLine(Environment.NewLine);
                    label5.Text = DateTime.Now.ToString();
                    label5.Visible = true;
                    w.WriteLine(label5.Text.ToString());
                    w.Close();
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> results = new List<string>();
            HtmlElementCollection elems = wb.Document.GetElementsByTagName("INPUT");
            foreach (HtmlElement elem in elems)
            {
                String nameStr = elem.GetAttribute("value");
                results.Add(nameStr);
            }  
        }

        bool sent = false;
        int count = 0;

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                count += 1;
                if (sent == true && count >= 60)
                {
                    count = 0;
                    timer1.Enabled = false;
                    webBrowser1 = new WebBrowser();
                    if (webBrowser1.IsBusy == false)
                    {
                        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
                        webBrowser1.Navigate("https://www.facebook.com/");
                    }
                    sent = false;
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
        }

        private StringBuilder builder;
        private static Random random = new Random((int)DateTime.Now.Ticks);
        private string RandomString(int size)
        {
            try
            {
                builder = new StringBuilder();
                char ch;
                for (int i = 0; i < size; i++)
                {
                    ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    builder.Append(ch);
                }
            }
            catch(Exception err)
            {
                string myerr = err.ToString();
            }
            return builder.ToString();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西:

var client = new FacebookClient(mytokenaslip);

//1-25
dynamic result = client.Get("/me/posts", new { limit = "25", offset = "0"});

//26-50
dynamic result = client.Get("/me/posts", new { limit = "25", offset = "25"});

您还可以选择一次获得超过25个帖子。

//51-100
dynamic result = client.Get("/me/posts", new { limit = "50", offset = "50"});

答案 1 :(得分:0)

您可以使用“recursive function”获取所有条目,API结果中的“next”参数包含下一批结果的API调用:https://developers.facebook.com/docs/graph-api/using-graph-api#paging

但请注意,如果尝试执行此操作太快且结果太多,则可能会达到API限制。此外,由于您要删除旧条目并删除一个条目是一个API调用,您应该在每次调用后尝试超时,以确保不会达到限制。

确保你学习并理解递归函数的工作原理,这是无数线索之一:Help with Creating a Recursive Function C#