Platinum Partner

Using Twitter via .NET - A Beginner's Guide

 

Objective :

In this post i will quickly explain how can we interact with Twitter API using .Net code.

Ingredients :

LinqToTwitter, Visual Studio 2010

Recipe :

You may find several popular Dlls on internet that communicate with Twitter. I have used LinqToTwitter & have found it easy to understand & implement.

You need to have “twitterConsumerKey” & “twitterConsumerSecret” that you will use from within your application. Get your applications twitter credentials from https://dev.twitter.com/apps

.NET code

Create a simple Asp.net web application in visual studio 2010.

Web.Config Changes

Add following in your web.config appSettings

<add key=”twitterConsumerKey” value=”twitterConsumerKey“/>

<add key=”twitterConsumerSecret” value=”twitterConsumerSecret“/>

Namespace

Add following namespace in your code file.

using LinqToTwitter;

Refer Code Snippet.

 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 && Request.QueryString["oauth_token"] != null)
            {
                auth.CompleteAuthorization(Request.Url);
            }

            if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||
                string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
            {
             //Do nothing
            }
            else if (auth.IsAuthorized)
            {
             //Do nothing
            }
            else
            {
                auth.BeginAuthorization(Request.Url);

                if (!auth.IsAuthorized)
                {
                    Uri specialUri = new Uri(Request.Url.ToString());
                    auth.BeginAuthorization(specialUri);
                }
            }

            if (auth.IsAuthorized)
            {
                twitterCtx = new TwitterContext(auth);

               #region Search Text
                var searchResult =
                    (from srch in twitterCtx.Search
                     where srch.Type == SearchType.Search &&
                           srch.Query == "Brian Adams"
                     select srch)
                    .SingleOrDefault();
                gvTweetView.DataSource = searchResult.Statuses;
                gvTweetView.DataBind();
	            #endregion

                #region UserInfo
                var users =
                               (from user in twitterCtx.User
                                where user.Type == UserType.Lookup &&
                                      user.ScreenName == "prosarfraz"
                                select user);

                string strHtml = string.Empty;


                foreach (User objUser in users)
                {

                    strHtml += "";
                    strHtml += " Screen Name ";
                    strHtml += "" + objUser.ScreenName + "";
                    strHtml += " Favorites Count ";
                    strHtml += "" + objUser.FavoritesCount + "";
                    strHtml += " Followers ";
                    strHtml += "" + objUser.FollowersCount + "";
                    strHtml += " Description ";
                    strHtml += "" + objUser.Description + "";
                    strHtml += "";

                }
                ulsrc.InnerHtml = "" + strHtml + "
"; #endregion } }
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}