Authorize and Post on Twitter Using Twitterizer

This article briefly describes how to authorize and post on Twitter using Twitterizer in ASP.Net with C#.

First of all you need to install Twitterizer using the package manager console or you can add reference manually.

img1.jpg

Image 1.

Now you must create a new application on Twitter and fill in the required details and accept the terms and supply the security code.

img2.jpg

Image 2.

Note: You need to enable Read and Write access type in the settings tab.

img3.jpg

Image 3.

As you can see, the consumer key, secret and token has been generated in the details tab.

Now let's work on the UI.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Twitterizer;

 

public partial class PostOnTwitter : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        CheckAuthorization();

    } 

    private void CheckAuthorization()

    {

        var oauth_consumer_key = "                               ";

        var oauth_consumer_secret = "                                             ";

 

        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,

                "India clinched the first Test against Australia with a comfortable eight-wicket victory new.");

 

            if (response.Result == RequestResult.Success)

            {

                Response.Write("Did it yaar");

            }

            else

            {

                Response.Write("Try some other time");

            }

        }       
    }   
}


Now run the application to see the result.

img4.jpg

Image 4.

Supply the credentials and click "Authorize app".

if (response.Result == RequestResult.Success)

            {

                Response.Write("Did it yaar");

            }

            else

            {

                Response.Write("Try some other time");

            }

This code shows the status of your post message.

img5.jpg

Image 5.
 


Similar Articles