Tweet on Behalf of Twitter User using ASP.Net C#

In this tutorial, I walk you through the end-to-end process of creating a Twitter application, generating an access token for a user and posting on behalf of that user. As you have probably figured out, the Twitter API is a horrible, horrible mistake of a mess. There are a bunch of random versions, the documentation is vague and the examples that you are able to find are likely using the old API which is now crippled.

I hope this tutorial saves you time and hair. It is the most complete .Net Twitter example I have seen to date for something that should be incredibly fast and easy to do.

What we are building

We are building a web application that allows our users to grant us access to tweet on their behalf. Once we have their permission, we create a tweet on the user's feed!

How we will build

Follow my instructions in the following video and you will be sending tweets for your clients in no time! It is easy to do, but there are some gotchas that this video will help you overcome!



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using Twitterizer;
 
namespace PostFansTwitter
{
    public partial class twconnect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var oauth_consumer_key = "gjxG99ZA5jmJoB3FeXWJZA";
            var oauth_consumer_secret = "rsAAtEhVRrXUTNcwEecXqPyDHaOR4KjOuMkpb8g";
 
            if (Request["oauth_token"] == null)
            {
                OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
                    oauth_consumer_key,
                    oauth_consumer_secret,
                    Request.Url.AbsoluteUri);
 
                Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}",
                    reqToken.Token));
            }
            else
            {
                string requestToken = Request["oauth_token"].ToString();
                string pin = Request["oauth_verifier"].ToString();
 
                var tokens = OAuthUtility.GetAccessToken(
                    oauth_consumer_key,
                    oauth_consumer_secret,
                    requestToken,
                    pin);
 
                OAuthTokens accesstoken = new OAuthTokens()
                {
                    AccessToken = tokens.Token,
                    AccessTokenSecret = tokens.TokenSecret,
                    ConsumerKey = oauth_consumer_key,
                    ConsumerSecret = oauth_consumer_secret
                };
 
                TwitterResponse<TwitterStatus> response = TwitterStatus.Update(
                    accesstoken,
                    "Testing!! It works (hopefully).");
 
                if (response.Result == RequestResult.Success)
                {
                    Response.Write("we did it!");
                }
                else
                {
                    Response.Write("it's all bad.");
                }
            }
        }
    }
}

Get Last 100 Posts From Twitter

Trevor, one of my YouTube subscribers, wanted an example for pulling in twitter posts with the ID and such. Here it is!

   1:  TwitterResponse<TwitterStatusCollection> col = TwitterTimeline.UserTimeline(accessToken, new UserTimelineOptions() { ScreenName = cs.ScreenName, Count = 100 });
   2:   
   3:  if (col != null && col.ResponseObject != null)
   4:  {
   5:      foreach (TwitterStatus co in col.ResponseObject)
   6:      {
   7:          //co.RetweetCount;
   8:          //co.Text;
   9:          //co.CreatedDate;
  10:          //co.Id.ToString();
  11:      }
  12:  }