Search this site

Monday, November 14, 2011

Build your own Twitter Application in C-sharp/C# dot net (.NET).



This blog will help you create an application that will allow you to work with Twitter from your website.

The application is build according to Twitter OAuth 1.0a.


For this you need to register the website for sending request that can be done from here https://dev.twitter.com/apps/new

You can give any live site URL here it will not accept local URL. Also describe the call back URL in case you intend to set request from a web application/site

Once you create you application you will get Consumer Key & Consumer Secret Key these will be used by you application (below).

Once you application is created go to the setting tab and select the application option as below in case you intend to tweet.


Now for creating a application follow these steps:

1) Download the twitter library(twittervb.dll) http://twittervb.codeplex.com/

2) Add reference of this dll in your .net web application.



private String ConsumerKey = "xxxxxxxxxxxxxxxxxxxxxxx";
private String ConsumerKeySecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";
TwitterAPI twitterAPI = new TwitterAPI();
if (String.IsNullOrEmpty(Request["oauth_token"]))
{
// Send the browser to Twitter for authentication along with a callback URL
Response.Redirect(twitterAPI.GetAuthorizationLink(ConsumerKey, ConsumerKeySecret, "http://localhost:1592/default.aspx"));
}
else
{
// Exchange the request token for an access token
twitterAPI.GetAccessTokens(ConsumerKey, ConsumerKeySecret, Request["oauth_token"], Request["oauth_verifier"]);
// You've got all the tokens now. You can write them to a database, cookies, or whatever
twitterAPI.AuthenticateWith(ConsumerKey, ConsumerKeySecret, twitterAPI.OAuth_Token, twitt

erAPI.OAuth_TokenSecret);
// Get some tweets from their timeline
foreach (TwitterStatus tweet in twitterAPI.HomeTimeline())
{
Response.Write("" + tweet.User.ScreenName + ":" + tweet.Text + "
"
);
}
}
twitterAPI.Update("You have Done it!!");
3) Update http://localhost:1592/default.aspx with you URL.
4) Run the .net web application.


Since this URL is different from the one that was used at the time of application creation Twitter will ask to confirm the request each time


You need to enter the credential and confirm the request click Authorize app.
5) Congratulations!! You have read some tweets as well tweeted something.

No comments:

Post a Comment