C#推文中的Twitter API

时间:2014-03-25 19:45:32

标签: twitter tweetstream

我正在使用twitter身份验证API,但我无法获得超过20条推文。 任何帮助请我的代码是

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_count = "100";
    var oauth_signature_method = "HMAC-SHA1";

    // unique request details
    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.Local);
    var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();


    // create oauth signature
    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 = true;

    // make the request

    var postBody = "q=" + Uri.EscapeDataString(q);

    resource_url += "?" + postBody +" count=10";// without this count api is returning 20 tweets but by using count the api return error.

    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 = "";

    try
    {
        JArray jsonDat = JArray.Parse(objText);

        html = jsonDat.Count().ToString();
        for (int x = 0; x < jsonDat.Count(); x++)
        {
            html += jsonDat[x]["status"]["text"].ToString() + "<br/>";
            html += "<a href='" + jsonDat[x]["url"].ToString() + "'>" + jsonDat[x]["url"].ToString() + "</a><br/>";
           // html += jsonDat[x]["user"].ToString() + "<br/>";
           // html += jsonDat[x]["ScreenName"].ToString() + "<br/>";
           //html += jsonDat[x]["User.ProfileImageUrl"].ToString() + "<br/>";
            //html += jsonDat[x]["created_at"].ToString() + "<br/>";
        }

      myDiv.InnerHtml += html;

      //Div1.InnerHtml = html;
    }
    catch (Exception twit_error)
    {
        myDiv.InnerHtml += html + twit_error.ToString();
    }
}

1 个答案:

答案 0 :(得分:1)

我不确定你在寻找什么,一个流或一个时间轴。 我是Tweetinvi的开发人员,可以让您轻松访问这两个功能。

以下是一个例子:

TwitterCredentials.SetCredentials("ACCESS_TOKEN", "ACCESS_TOKEN_SECRET", "CONSUMER_KEY", "CONSUMER_SECRET");
var tweets = Timeline.GetHomeTimeline(100);

我认为这应该可以减轻您的发展。