Saturday, April 18, 2009

Twitter Posting from Asp.net Website - Twitterizer Open Source Tutorial

Please refer to new way of posting tweet using Twitter Api, following is old way of posting twitter message and this method is no more supported by twitter.

http://dotnetguts.blogspot.com/2010/05/tweet-posting-from-aspnet-using-oauth.html

Before started discussing about Twitter Programming.  Following is small description about Twitter.  "Twitter is a service for friends, family, and co–workers to communicate and stay connected through the exchange of quick, frequent answers to one simple question"  As Twitter is gaining more and more popularity, it is necessary for web master to put all the websites update on Twitter to attract more audience. 

This post will explain, How to post message from your asp.net website to Twitter.

To demonstrate this, I would be taking help of Open Source Project which easy, fast and efficient to use.  

Open Source Project for Twitter Programming in .NetTwitterizer


Practical Example for Twitter Programming in .Net


Step2: Create New Asp.net Website. 

Step 3: Arrange the Web Controls as shown in Figure.

Step 4: Add Twitterizer Reference.
Right Click Website in Solution Explorer, and select "Add Reference" from Popup dialog.
Add the Twitterizer.Framework.dll which you have downloaded in Step1.

Step 5: Write Code.
Add Namespace: using Twitterizer.Framework;

Add following code on Shout button click event.
protected void btnShout_Click(object sender, EventArgs e)
{   
string TwitterUserName = "twitasp";
string TwitterPassword = "mypassword";
string TwitterMessage = txtShout.Text;

if (TwitterUserName != string.Empty && TwitterPassword != string.Empty)
{
try
{

Twitter twitter = new Twitter(TwitterUserName,TwitterPassword);

string TwitterMsg = txtShout.Text;
if (txtShout.Text.Length > 120)
{
TwitterMsg = TwitterMsg.Substring(0, 130) + "... For more update logon to DailyFreeCode.com";
}
else
{
TwitterMsg = TwitterMsg + "... For more update logon to DailyFreeCode.com";
}
twitter.Status.Update(TwitterMsg);
lblTwitMsg.Text = "Your have shout successfully on http://twitter.com/" + TwitterUserName;

}
catch (Exception ex)
{
Response.Write("<b>" + ex.Message + "</b><br>" + ex.StackTrace);
}
}
}

Lets Post one Message from Asp.net Website Developed.
Type your Message in Textbox and Press Shout button, you can then check your Twitter website to check whether update is applied or not.


Now Press "Shout on Twitter!" button


Now check the Twitter Website for Profile we have Shout on.
http://twitter.com/twitasp


Helpful links for Twitter Programming in .Net
Twitter API Documentation:http://apiwiki.twitter.com/
Twitter Open Source Example: http://code.google.com/p/twitterizer/
Online Discussion Group for Twitterizer: http://groups.google.com/group/twitterizer/


25 comments:

Akash Shah said...

Hey Vivek

Thats what I was looking for.

Thanx a lot.

DotNetGuts said...

You are welcome man!

Mushtaq Bunir said...

hi,
I have a problem with the api,
i want to post updates on the twitter.com from my site, but it gives the error code 401 unauthorized...
what is it ?
plz help me..

DotNetGuts said...

@Mushtaq,

Its not possible unless you have proper authorization. It is working perfect on my machine. Check your twitter username and password.

Rajni said...

Hello,
I want to set status as favourite from asp.net api

I do not found any way to do this,

Can you please help me?

Vijayakumar Veeraragava Pargunan said...

Hi,

I got the following error message, but its updated the Twitter.

Error Parsing Twitter Response.

Please advice

Thanks
vijay

DotNetGuts said...

It should not be the case, as its working for me.

but try out following function.

public void TwitterPostIt(string Message)
{
string TwitterUserName = "YourTwitterUserName";
string TwitterPassword = "YourTwitterPassword";

try
{
if (TwitterUserName != string.Empty && TwitterPassword != string.Empty)
{

TwitterPassword = clsGeneral.Decrypt(TwitterPassword, true);
Twitter twitter = new Twitter(TwitterUserName, TwitterPassword);

string TwitterMsg = Message;
if (Message.Length > 120)
{
TwitterMsg = TwitterMsg.Substring(0, 120) + " visit us at http://dailyfreecode.com";
}
else
{
TwitterMsg = TwitterMsg + TwitterTrailingMsg;
}
twitter.Status.Update(TwitterMsg);
}
}
catch (Exception ex)
{
}
}

Vijayakumar Veeraragava Pargunan said...

Hi,
I tried TwitterPostIt function, same error will shown.

Error :
Error Parsing Twitter Response.
at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 87 at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 43 at Twitterizer.Framework.TwitterStatusMethods.Update(String Status, Nullable`1 InReplyToStatusID) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\Method Classes\TwitterStatusMethods.cs:line 157 at Twitterizer.Framework.TwitterStatusMethods.Update(String Status) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\Method Classes\TwitterStatusMethods.cs:line 129 at _Default.TwitterPostIt(String Message) in c:\Documents and Settings\vijay\My Documents\Visual Studio 2008\WebSites\Microblog\Default.aspx.cs:line 70

please advice.

Thanks
Vijay

DotNetGuts said...

@Vijay,

Could you debug your code and check are you using

1. Proper username and password for twitter.
2. First try using with smaller lenght string. eg: "Hello World"
3. Try using long string.

As i don't have control over your code, i can only suggest you to debug it and let me know where exactly exception occurred while using twitter function.

pangitson said...

Actually It runs,

Just comment the catch ex.Message. Then cut the free download text.. IT really runs.. Hehe tanks a lot.. BEst Regards to the commentor..

Rebeca said...

I am new as a twitter user, so it is very important for me. Many many thanks for this tutorial.

P Anil said...

i have the same problem like vijay and if i remove try catch blog it works fine but if i add try catch blog why the code goes to catch blog and the message send successfull.
and tell what we do more using this DLL. thanx for your commants in advance i am wating.
:)

like this error is
Twitterizer.Framework.TwitterizerException: Error Parsing Twitter Response. ---> Twitterizer.Framework.TwitterizerException: Error Parsing Twitter Response. ---> System.OverflowException: Value was either too large or too small for an Int32.
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at Twitterizer.Framework.TwitterRequest.ParseStatusNode(XmlNode Element) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 164
at Twitterizer.Framework.TwitterRequest.ParseResponseData(TwitterRequestData Data) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 108
--- End of inner exception stack trace ---
at Twitterizer.Framework.TwitterRequest.ParseResponseData(TwitterRequestData Data) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 139
at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 80
--- End of inner exception stack trace ---
at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 87
at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\TwitterRequest.cs:line 43
at Twitterizer.Framework.TwitterStatusMethods.Update(String Status, Nullable`1 InReplyToStatusID) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\Method Classes\TwitterStatusMethods.cs:line 157
at Twitterizer.Framework.TwitterStatusMethods.Update(String Status) in C:\Documents and Settings\benjaminp\My Documents\Visual Studio 2008\Projects\twitterizer\Twiterizer.Framework\Method Classes\TwitterStatusMethods.cs:line 129
at _Default.Button1_Click(Object sender, EventArgs e) in d:\TwitterDemo\Default.aspx.cs:line 30}

Joshua Blackstone said...

If you're having issues, I was too. It was telling me that it was missing a file. Anyways, if you go to: http://code.google.com/p/twitterizer/ and download the latest version (Twitterizer.Framework-1.0.1.120.zip), and use the same code as this article, it will work without error.
Good article, thanks!

Soumyabrata Paul said...

Thanks a lot for the post. I have tried this and the code is working perfectly all right for me. This amazing code and thanks again for the post.

Keep it up. Wish you all Merry Christmas.

Unknown said...

hi
is it possible to get redirected to twitter login page an after entering proper credentials msg should get posted?

Scher said...

Hi, it doesn't work correctly for me. When I want to publish, there is this error: "Error Parsing Twitter Response." but work, i mean, it publish in twitter, but then, show me this error.
And when I want to publish de same text than the last time, it doesn't work, it doesn't publish anything and show me this error: "The remote server returned an error: (403) Forbidden."

Please, Help me, I need to fix it for my job.

Sorry for my english, but Im from Argentina

Rohini said...

i got error, "Authorization Failed". plz giv me d answer. thanx

Unknown said...

How can i Shout my Message on to Twitter using oAuth Library, as the above code is not working for me?

DotNetGuts said...

Check out this Twitter Tutorial, this will explains you, how to post tweet using OAuth

Unknown said...

guys please help:
it is showing the following error

TwitterizerException: Authorization Failed]
Twitterizer.Framework.TwitterRequest.HandleWebException(TwitterRequestData Data, WebException wex) +467
Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) +553
Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data) +35
Twitterizer.Framework.TwitterStatusMethods.Update(String Status, Nullable`1 InReplyToStatusID) +360
Twitterizer.Framework.TwitterStatusMethods.Update(String Status) +93
_Default.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\cs0060510\My Documents\Visual Studio 2008\WebSites\fortwitter\Default.aspx.cs:93
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1746

Unknown said...

Authorization Failed

at

Twitterizer.Framework.TwitterRequest.HandleWebException(TwitterRequestData Data, WebException wex) at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data) at Twitterizer.Framework.TwitterStatusMethods.Update(String Status, Nullable`1 InReplyToStatusID) at Twitterizer.Framework.TwitterStatusMethods.Update(String Status) at tweetdemo.Button1_Click(Object sender, EventArgs e) in d:\Dummy procs\prac4perf\tweetdemo.aspx.cs:line 44

Unknown said...

twitter.Status.Update(TwitterMsg);

NOT WORKING

AUTHORISATION FAILED

prasad g said...

i also tried but i am also getting same error...
The remote is not fount like that


The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized. at System.Net.HttpWebRequest.GetResponse() at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) --- End of inner exception stack trace --- at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data, String HTTPMethod) at Twitterizer.Framework.TwitterRequest.PerformWebRequest(TwitterRequestData Data) at Twitterizer.Framework.TwitterStatusMethods.Update(String Status, Nullable`1 InReplyToStatusID) at Twitterizer.Framework.TwitterStatusMethods.Update(String Status) at Share.ImageButton1_Click(Object sender, ImageClickEventArgs e) in d:\Prasad\Work\Share.aspx.cs:line 44

sarathyselvaraj said...

Sarathy


i am using the correct twitter username password it was properly login my twitter but when
i tried the same code as well as the uname, pwd,i am getting the Authentication Failed Error

can you please guide me to solve this issue

TARUN said...

hi thanks for ur post.. how can i download Twitterizer API.. when i clik on the hyper links in the first step, it shows "page is not available"..

plz help me....
thanks in advance

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape