需要帮助的关于Twitter的搜索和流API

需要帮助的关于Twitter的搜索和流API

本文介绍了需要帮助的关于Twitter的搜索和流API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#和asp.net一个新手。
我试图创建一个简单的网站(在本地主机上运行),它使用流媒体和搜索Twitter的API。

I'm a newbie in C# and asp.net.I'm trying to create a simple web(run on localhost) which use streaming and search API of Twitter.

我试着像一些查询:的\"但它出现了错误的验证数据的一些错误。

I've tried some query like: "https://api.twitter.com/1.1/statuses/mentions_timeline.json?count=2&since_id=14927799" but it got some error about "Bad Authentication data".

可能是我不知道怎么去验证。所以,任何人都可以帮助我吗?感谢很多。

May be I don't know how to get Authentication. So could anyone help me? THanks a lots.

推荐答案

你所表现出不是搜索功能,而是流功能。
这里是工作的一个完整的例子叽叽喳喳流搜索

What you have shown is not the search functionality, rather is the stream functionality.Here is a complete example of working twitter stream search

    private void SearchTweetsWithQuery(string query)
    {
        var oauth_token = System.Configuration.ConfigurationManager.AppSettings["accessToken"];
        var oauth_token_secret = System.Configuration.ConfigurationManager.AppSettings["accessTokenSecret"];
        var oauth_consumer_key = System.Configuration.ConfigurationManager.AppSettings["consumerKey"];
        var oauth_consumer_secret = System.Configuration.ConfigurationManager.AppSettings["consumerSecret"];

        // oauth implementation details
        var oauth_version = "1.0";
        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.Utc);
        var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

        // message api details
        var status = "Updating status via REST API if this works";
        var resource_url = "https://api.twitter.com/1.1/search/tweets.json";
        var screen_name = "TODO : UPDATE THIS OR LEAVE BLANK";

        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,
                                    query
                                    );

        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_consumer_key=\"{3}\", oauth_nonce=\"{0}\", oauth_signature=\"{5}\", oauth_signature_method=\"{1}\", " +
                           "oauth_timestamp=\"{2}\",  " +
                           "oauth_token=\"{4}\", " +
                           "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)
                        );


        // make the request

        ServicePointManager.Expect100Continue = false;

        var postBody = "screen_name=" + Uri.EscapeDataString(screen_name);//
        resource_url += "?q=" + query;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Headers.Add("Authorization", authHeader);
        request.Method = "GET";
        request.ContentType = "application/x-www-form-urlencoded";

        WebResponse response = request.GetResponse();
        string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
    }

这篇关于需要帮助的关于Twitter的搜索和流API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:25