繁体   English   中英

无法控制来自C#,Twitter REST API的Twitter搜索结果的大小

[英]Cannot control size of twitter search result from C#, twitter REST API

我正在使用C#通过REST API收集推文。 Search API具有count参数以控制结果的大小。

我不知道为什么该参数超过15时将不起作用。

当count的值小于15时,它可以正常工作。(3、5,依此类推。)如果超过15,则仍仅显示15条推文。

var timelineUrl =“ https://api.twitter.com/1.1/search/tweets.json?q=test&count=17 ”;

这是我的代码。

        var authHeaderFormat = "Basic {0}";

        var authHeader = string.Format(authHeaderFormat,
            Convert.ToBase64String(Encoding.UTF8.GetBytes(Uri.EscapeDataString("") + ":" +
            Uri.EscapeDataString(("")))
        ));
        var postBody = "grant_type=client_credentials";

        HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create("https://api.twitter.com/oauth2/token");
        authRequest.Headers.Add("Authorization", authHeader);
        authRequest.Method = "POST";
        authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

        using (System.IO.Stream stream = authRequest.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }

        authRequest.Headers.Add("Accept-Encoding", "gzip");

        WebResponse authResponse = authRequest.GetResponse();
        // deserialize into an object
        TwitAuthenticateResponse twitAuthResponse;
        using (authResponse)
        {
            using (var reader = new StreamReader(authResponse.GetResponseStream()))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objectText = reader.ReadToEnd();
                twitAuthResponse = JsonConvert.DeserializeObject<TwitAuthenticateResponse>(objectText);
            }
        }

        // Do the timeline
        var timelineFormat = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name={0}&include_rts=1&exclude_replies=1&count=5";
        //var timelineUrl = string.Format(timelineFormat, "shj860728");
        var timelineUrl = "https://api.twitter.com/1.1/search/tweets.json?q=test&count=17";
        HttpWebRequest timeLineRequest = (HttpWebRequest)WebRequest.Create(timelineUrl);
        var timelineHeaderFormat = "{0} {1}";
        timeLineRequest.Headers.Add("Authorization", string.Format(timelineHeaderFormat, twitAuthResponse.token_type, twitAuthResponse.access_token));
        timeLineRequest.Method = "Get";
        WebResponse timeLineResponse = timeLineRequest.GetResponse();
        var timeLineJson = string.Empty;
        using (timeLineResponse)
        {
            using (var reader = new StreamReader(timeLineResponse.GetResponseStream()))
            {
                timeLineJson = reader.ReadToEnd();
            }
        }
        //foreach(var tweet : )

        JObject result = JObject.Parse(timeLineJson);
        IList<JToken> results = result["statuses"].Children().ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM