Tuesday, June 4, 2013

Tweet Posting from Asp.net using OAuth - Twitterizer Tutorial


Twitter Tweet Posting from Asp.net Web Application using OAuth.

Main Steps
1) Create Twitter Application.
2) Post Tweet from Asp.net Website using Twitterizer Api

Now, lets understand each steps in more details, step by step.

Create Twitter Application
1) Open http://twitter.com/apps/new
2) Login to twitter and Fill the Application Information as follow.






















 - Choose Image for your Twitter Application.
 - Fill in Twitter Application Name.
 - Description of Twitter Application
 - Fill in Application Website name, Organization
























- Choose Application Type as "Browser" as we are developing asp.net website to post tweet.
- Callback Url is url where twitter would redirect after authentication.
    - Fill in rest of details as shown in figure and your Twitter Application is ready to use.























Now, lets begin the important part, how to post tweet.

Post Tweet from Asp.net Website using Twitterizer Api
1) Create Asp.net Application
2) Download Twitterizer
3) Add Reference of Twitterizer.dll files to newly created asp.net web application
4) Copy Consumer Key and Consumer Secret generated from Twitter Application.
5) Add Consumer Key and Consumer Secret to web.config file inside AppSettings

    <add key="consumerKey" value="XXXXXXXXXx"/>
    <add key="consumerSecret" value="XXXXXXXXXXXXXXXX"/>

6) In .aspx page arrange the controls as shown in figure.















7) Paste following code in "Authenticate User" Button Click event

protected void btnAuthenticate_Click(object sender, EventArgs e)
{
// add these to web.config or your preferred location
var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

//If User is not valid user
if (Request.QueryString["oauth_token"] == null)
{
//Step 1: Get Request Token
OAuthTokenResponse RequestToken 
= OAuthUtility.GetRequestToken(consumerKey,                                                 consumerSecret);

//Step 2: Redirect User to Requested Token
Response.Redirect("http://twitter.com/oauth/authorize?oauth_token=" 
+ RequestToken.Token);
}
else
{
//For Valid User
string Oauth_Token = Request.QueryString["oauth_token"].ToString();
var accessToken = OAuthUtility.GetAccessToken(consumerKey,
consumerSecret,
Oauth_Token);

lblMessage.Text = "<b>Hello " 
+ accessToken.ScreenName 
+ ", Welcome to Go4Sharepoint.com Twitter App<b>";

lblMessage.Text += "<br/> Token: " + accessToken.Token;
lblMessage.Text += "<br/> TokenSecret: " + accessToken.TokenSecret;
lblMessage.Text += "<br/> UserId: " + accessToken.UserId;
}
}

8) Now run the application and try to click on "Authenticate User" button, it will redirect you to twitter website authenticate user and return back to your asp.net website.


















Click on Authenticate User button.













Press on Allow button after you enter your twitter login account details




















9) Create OAuthToken Object and Post Tweet.
protected void btnPostTweet_Click(object sender, EventArgs e)
{
// add these to web.config or your preferred location
var consumerKey = ConfigurationManager.AppSettings["consumerKey"];
var consumerSecret = ConfigurationManager.AppSettings["consumerSecret"];

OAuthTokens accessToken = new OAuthTokens();
accessToken.AccessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
accessToken.AccessTokenSecret = "xxxxxxxxxxxxxxxxxxxx";
accessToken.ConsumerKey = consumerKey;
accessToken.ConsumerSecret = consumerSecret;

TwitterStatus TweetStatus = new TwitterStatus(accessToken);
TweetStatus.Update(txtTweet.Text + " - http://www.Go4Sharepoint.com");
}

10) Type in your Message and Press "Shout on Twitter" button























Now, check your twitter account (Example here:http://twitter.com/aspnetmvp )

















Check out Live Demo of Tweet Posting from Asp.net

0 comments:

Post a Comment