Search code examples
c#twitterlinq-to-twitter

Can't search for tweets with LinqToTwitter


I'm starting out a new c# project where I want to extract some tweets, nothing specific at the moment, just to get started...

I created a developer user at the Twitter API and created a new app and got all the consumerkey and accesstoken...

According to this link - https://github.com/JoeMayo/LinqToTwitter/wiki/Single-User-Authorization Everything should work fairly smooth but I just don't get any results...

My project is a .net console application and this is my code which is almost copy-paste from the reference I mentioned above:

var auth = new SingleUserAuthorizer
{
    CredentialStore = new SingleUserInMemoryCredentialStore
    {
        ConsumerKey = ...,
        ConsumerSecret = ...,
        AccessToken = ...,
        AccessTokenSecret = ...
    }
};


var twitterCtx = new TwitterContext(auth);

var searchResponse =
    (from search in twitterCtx.Search
     where search.Type == SearchType.Search &&
           search.Query == "\"LINQ to Twitter\""
     select search)
    .SingleOrDefault();

if (searchResponse != null && searchResponse.Statuses != null)
    searchResponse.Statuses.ForEach(tweet =>
        Console.WriteLine(
            "User: {0}, Tweet: {1}",
            tweet.User.ScreenNameResponse,
            tweet.Text));

I don't even know how to identify if the authentication process was successful, Any assistance please? Thanks!


Solution

  • You missed the await on the search query:

    var searchResponse =
        await
        (from search in twitterCtx.Search
         where search.Type == SearchType.Search &&
               search.Query == "\"LINQ to Twitter\""
         select search)
        .SingleOrDefaultAsync();
    

    LINQ to Twitter is async, which means that all methods in the call chain must be async and you must await calls. What is probably happening is that the method returns before the searchResponse is populated and you don't see results. Adding the await makes the code wait for the response.