Tweetsharp搜索功能

本文关键字:功能 搜索 Tweetsharp | 更新日期: 2023-09-27 18:12:54

我写了一小段代码,不知道哪里出错了。我所要做的就是使用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();
}

Tweetsharp搜索功能

我在我的WP7应用程序中做同样的事情,这里是一些适用于我的代码(你似乎有真实的部分ok。我在异步方法调用中使用委托。检查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()));
        }
    });

你这里有什么问题?什么不行?

在我看来,你有一些额外的代码,你不需要,但它应该与此工作:

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();

}