Search code examples
c#twittertwitter-oauthlinq-to-twitter

Get Tweets by LinqToTwitter


I'm trying to get tweets from Twitter but it's not working with me, here is the code:

var auth = new SingleUserAuthorizer
            {

                CredentialStore = new SingleUserInMemoryCredentialStore()
                {

                    ConsumerKey = ConfigurationManager.AppSettings["***"],
                    ConsumerSecret = ConfigurationManager.AppSettings["***"],
                    AccessToken = ConfigurationManager.AppSettings["***"],
                    AccessTokenSecret = ConfigurationManager.AppSettings["***"]

                }

            };

var context = new TwitterContext(auth);
var tweets =
    from tw in context.Status
    where
        tw.Type == StatusType.User &&
        tw.ScreenName == "***"
    select tw;
// handle exceptions, twitter service might be down
try
{
    // map to list
    tweets
        .Take(3)
        .Select(t =>
            new Tweets
            {
                //Username = t.ScreenName,
                //FullName = t.User.Name,
                TweetText = t.Text,
                //FormattedText = ParseTweet(t.Text)
            })
        .ToList();
}
catch (Exception) { }

every time it fail when I'm trying to read the tweets, the exception is

LinqToTwitter.TwitterQueryException: Bad Authentication data

But I'm sure that the credentials are correct.

and also is it possible to read the posts of another twitter account? like a company account or a celebrate account?


Solution

  • LINQ to Twitter is async, so you should change your query like this:

    var tweets =
        await
        (from tw in context.Status
         where
            tw.Type == StatusType.User &&
         tw.ScreenName == "***"
         select tw)
        .ToListAsync();
    

    Also, hit a breakpoint after instantiating auth and inspect Credentials to make sure you've populated them correctly.