Tweetsharp搜索功能

时间:2013-05-09 09:38:05

标签: c# asp.net tweetsharp

我写了一小段代码,无法解决我出错的地方。我想做的就是使用tweetsharp在twitter上执行搜索并返回结果。

以下是代码:

protected void Page_Load(object sender, EventArgs e){
    _consumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
    _consumerSecret = ConfigurationManager.AppSettings["ConsumerKey"];
    _accessToken = ConfigurationManager.AppSettings["accessToken"];
    _accessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"];

    TwitterClientInfo twitterClientInfo = new TwitterClientInfo();
    twitterClientInfo.ConsumerKey = _consumerKey;
    twitterClientInfo.ConsumerSecret = _consumerSecret;

    TwitterService service = new TwitterService(twitterClientInfo);

    //var service = GetAuthenticatedService();

    OAuthRequestToken requestToken = service.GetRequestToken();

    requestToken.OAuthCallbackConfirmed = true;
    requestToken.Token = _accessToken;
    requestToken.TokenSecret = _accessTokenSecret;

    Uri authUrl = service.GetAuthenticationUrl(requestToken);

    service.AuthenticateWith(_accessToken, _accessTokenSecret);



    var tweets = service.Search(new SearchOptions { Q = "#MetGala", Count = 100 });

    if (tweets != null)
    {

        foreach (var tweet in tweets.Statuses)
        {
            System.Diagnostics.Debug.WriteLine("{0} says '{1}", tweet.User.ScreenName, tweet.Text);
        }
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("FAIL");
    }

    FacebookFeed();
}

2 个答案:

答案 0 :(得分:0)

你在这问题是什么?什么不起作用?

据我所知,你有一些你不需要的额外代码,但它应该可以解决这个问题:

protected void Page_Load(object sender, EventArgs e){
_consumerKey = ConfigurationManager.AppSettings["ConsumerKey"];
_consumerSecret = ConfigurationManager.AppSettings["ConsumerKey"];
_accessToken = ConfigurationManager.AppSettings["accessToken"];
_accessTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"];

TwitterClientInfo twitterClientInfo = new TwitterClientInfo();
twitterClientInfo.ConsumerKey = _consumerKey;
twitterClientInfo.ConsumerSecret = _consumerSecret;

TwitterService service = new TwitterService(twitterClientInfo);

service.AuthenticateWith(_accessToken, _accessTokenSecret);



var tweets = service.Search(new SearchOptions { Q = "#MetGala", Count = 100 });

if (tweets != null)
{

    foreach (var tweet in tweets.Statuses)
    {
        System.Diagnostics.Debug.WriteLine("{0} says '{1}", tweet.User.ScreenName, tweet.Text);
    }
}
else
{
    System.Diagnostics.Debug.WriteLine("FAIL");
}

FacebookFeed();

}

答案 1 :(得分:0)

我在WP7应用程序中做同样的事情,这里有一些适合我的代码(你似乎有auth部分确定。我在asynch方法调用中使用委托。检查twitter搜索响应,你可能会看到你的错误:

    _twitterService = (App.Current as App).Twitter;

    var searchOptions = new SearchOptions { Q = _hashTag.Name, IncludeEntities = false, Resulttype = _twitterSearchResultType };

    if (_returnedResultsCount != null)
        searchOptions.Count = _returnedResultsCount;

    _twitterService.Search(searchOptions, (twitterSearchResult, twitterSearchResponse) =>
    {
        try
        {
            if (twitterSearchResponse.StatusCode == HttpStatusCode.OK)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(IEnumerable<TwitterStatus>));

                    ser.WriteObject(ms, twitterSearchResult.Statuses);

                    byte[] data = ms.ToArray();

                    result(new LoadRequestResult(new MemoryStream(data)));
                }
            }
            else
            {
                result(new LoadRequestResult(new MemoryStream()));
            }
        }
        catch
        {
            result(new LoadRequestResult(new MemoryStream()));
        }
    });