This project has moved. For the latest updates, please go here.

Get all tweets by a user and his retweets

Apr 11, 2012 at 11:10 AM

I'm trying to get all tweets for an account and the tweets retweeted by that account.

I'm trying like this but I can't make it work :

var twitterCtx = new TwitterContext();
var tweets = (from tweet in twitterCtx.Status
                                where tweet.ID == AccountID && (tweet.Type == StatusType.User || tweet.Type == StatusType.RetweetedByUser)
                                  select tweet).OrderByDescending(item => item.CreatedAt).Take(10);

Any help would be greately appreciated it.

Apr 11, 2012 at 3:40 PM


There's two items here: authorization and separating the query.  Here's an example:


            string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            var consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

            var auth = new PinAuthorizer()
                Credentials = new InMemoryCredentials
                    ConsumerKey = consumerKey,
                    ConsumerSecret = consumerSecret,

            var twitterCtx = new TwitterContext(auth);
            var tweetResponse =
                (from tweet in twitterCtx.Status
                 where tweet.Type == StatusType.User &&
                       tweet.ScreenName == "JoeMayo" &&
                       tweet.IncludeRetweets == true
                 select tweet)

            var tweets = tweetResponse.OrderByDescending(item => item.CreatedAt).Take(10);

            foreach (var tweet in tweets)
                Console.WriteLine(tweet.Text + "\n");

Many queries, such as this one, require OAuth authorization. Here, I'm using PinAuthorizer, but there are other authorizers for different technologies. i.e. WebAuthorizer for Web Forms, MVCAuthorizer for ASP.NET MVC, or SilverlightAuthorizer for Silverlight.  For more information, see Learning to Use OAuth with LINQ to Twitter.

Next, notice how I separated the query, one query for Twitter and another to shape the data.



Apr 11, 2012 at 3:56 PM

I'm using Webforms and I've been trying to do the OAuth, but can't manage to make it work, I always get an 401 error.

I have the ConsumerKey, ConsumerSecret, AccessToken and AccessTokenSecret from

¿Any example on how to create an Authorization for Webforms with that data?

Thanks in advance.

Apr 11, 2012 at 4:15 PM

I had to temporarily pull the existing examples because they're based on Public queries, which is being deprecated.  I'm updating samples, but it's a gradual process.  Here's a snippet from one of those old examples:

using System;
using System.Configuration;
using System.Linq;
using LinqToTwitter;

public partial class _Default : System.Web.UI.Page
    private WebAuthorizer auth;
    private TwitterContext twitterCtx;

    protected void Page_Load(object sender, EventArgs e)
        IOAuthCredentials credentials = new SessionStateCredentials();

        if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

        auth = new WebAuthorizer
            Credentials = credentials,
            PerformRedirect = authUrl => Response.Redirect(authUrl)

        if (!Page.IsPostBack)

        if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||
            // The user needs to set up the web.config file to include Twitter consumer key and secret.
        else if (auth.IsAuthorized)
            screenNameLabel.Text = auth.ScreenName;

        twitterCtx = auth.IsAuthorized ? new TwitterContext(auth) : new TwitterContext();

        var tweets =
            from tweet in twitterCtx.Status
            where tweet.Type == (auth.IsAuthorized ? StatusType.Friends : StatusType.Public)
            select tweet;

        TwitterListView.DataSource = tweets;

    protected override void OnPreRender(EventArgs e)

        if (twitterCtx != null)
            twitterCtx = null;

    protected void authorizeTwitterButton_Click(object sender, EventArgs e)

    protected void postUpdateButton_Click(object sender, EventArgs e)
        if (!Page.IsValid)

        updateBox.Text = string.Empty;

Also, there are various reasons for receiving a 401.  You might want to do a search on these forums to see if any comments help.


Apr 12, 2012 at 9:26 AM
Edited Apr 12, 2012 at 9:26 AM

The problem is that I don't have any button, when the page loads the first time the tweets should already be visible, so the BeginAuthorization and CompleteAuthorization all go in the same page_load.

Isn't there an easier way to do it without having to do so many callbacks?, since I already have the AccessToken and AccessTokenSecret from can't I use that somehow instead of having to authorize?

Thanks again,

Apr 12, 2012 at 2:22 PM

There are a couple way to handle this: Retain secrets from an initial authorization or use SingleUserAuthorizer.

If you run the application one time, you can get all of the credentials from TwitterContext.Authorizer.Credentials and save them for all subsequent runs.

Using SingleUserAuthorizer, you can visit your Twitter application page and request single user tokens to use along with your ConsumerToken and ConsumerSecret.  For more info, see the Implementing Single User Authorization docs.


Apr 12, 2012 at 3:33 PM
Edited Apr 12, 2012 at 3:35 PM

That's just what I was looking for. Implementing Single User Authorization is perfect, I browsed the documentation but couldn't find that page.

Thanks !