如何在c#中获取具体数量的最新推文详细信息

时间:2016-09-26 03:28:19

标签: c# asp.net .net twitter twitter-oauth

在我们的ASP.Net应用程序中,我们希望根据用户输入选择来提取推文。像去年5月10日/ 15月20日的推文一样。 所以想法是用户将从UI下拉菜单中选择值,并且该推文的数量将显示在UI上。 目前我可以提取所有推文,但如何只获取特定的推文?

  public void findUserTwitter(string resource_url, string q)
    {

        // oauth application keys
        var oauth_token = "-"; //"insert here...";
        var oauth_token_secret = ""; //"insert here...";
        var oauth_consumer_key = "";// = "insert here...";
        var oauth_consumer_secret = "";// = "insert here...";

        // oauth implementation details
        var oauth_version = "1.0";
        var oauth_signature_method = "HMAC-SHA1";

        var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        var timeSpan = DateTime.UtcNow
            - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

        var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                        "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&q={6}";

        var baseString = string.Format(baseFormat,
                                    oauth_consumer_key,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauth_version,
                                    Uri.EscapeDataString(q)
                                    );

        baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

        var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                "&", Uri.EscapeDataString(oauth_token_secret));

        string oauth_signature;
        using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
        {
            oauth_signature = Convert.ToBase64String(
                hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
        }

        // create the request header
        var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                           "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                           "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                           "oauth_version=\"{6}\"";

        var authHeader = string.Format(headerFormat,
                                Uri.EscapeDataString(oauth_nonce),
                                Uri.EscapeDataString(oauth_signature_method),
                                Uri.EscapeDataString(oauth_timestamp),
                                Uri.EscapeDataString(oauth_consumer_key),
                                Uri.EscapeDataString(oauth_token),
                                Uri.EscapeDataString(oauth_signature),
                                Uri.EscapeDataString(oauth_version)
                        );



        ServicePointManager.Expect100Continue = false;

        // make the request
        var postBody = "q=" + Uri.EscapeDataString(q);//
        resource_url += "?" + postBody;

        resource_url += "&count=5";


        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";
        var response = (HttpWebResponse)request.GetResponse();
        var reader = new StreamReader(response.GetResponseStream());
        var objText = reader.ReadToEnd();
        myDiv.InnerHtml = objText;/**/
        string html = "";
            JArray jsonDat = JArray.Parse(objText);
            html += "<table>";
            for (int x = 0; x < jsonDat.Count; x++)
            {
                html += " <tr><td>" + jsonDat[x]["text"].ToString() + "</td></tr>";
                html += " <tr><td>" + jsonDat[x]["created_at"].ToString() + " </ td ></ tr > ";
                //  html += " <tr><td>" + String.Format("{0:F}", Convert.ToDateTime(jsonDat[x]["created_at"])) + " </ td ></ tr > ";

            }
            html += " </table>";
            myDiv.InnerHtml = html;

    }

0 个答案:

没有答案