Twitter oAuth with .NET for the Desktop
I hope people have benefitted from my example application for Twitter oAuth in .NET. Occasionally someone will leave my Twitter name in the sample post and it pops up in my Tweetdeck. I don’t mind – it’s nice to know that the code is useful.
The original code that I provided was aimed at web applications. I haven’t had the need to develop any desktop apps lately and so I didn’t create any code for that style of oAuth. Quite frankly, I still find the desktop oAuth (using the PIN) to be a little awkward and I look forward to the promotion of xAuth by the Twitter team. However, someone asked me to create a sample desktop app using oAuth. I saw it as a challenge and decided to give it a try.
My first pass at the app popped up a web browser using ProcessInfo(). It worked, but I decided to go with the webcontrol to keep the user focused on the application. This form represents a one-time login page. After the login and PIN process, you can store the token and secret so that the user won’t need to login again (unless he or she revokes access for your application).
Once the user allows access to your application (screen shots above), a PIN will be displayed. The user must then enter that PIN in the textbox near the bottom of the screen. When the user clicks on the Update button, the PIN is used to retrieve the final credentials from Twitter.
When you add this to your project, you’ll need to include references to System.Web and System.Configuration. Other than that, it’s a pretty standard desktop application.
As I mentioned, I’m looking forward to Twitter’s general release of xAuth. That’s the type of authentication used by Seesmic Look. It’s a much smoother process for the user. Until then, I hope this code will help with your Twitter desktop applications.
p.s. – Of course I saw this post after I created my app (why does it always happen that way?). – GameTree – http://gametree.org/?p=245




Shannon Whitley said,
Wrote on February 18, 2010 @ 11:50 pm
I gave this some more thought and it occurred to me that I’m not doing enough to prove to the user that the login is going through Twitter. When I popped up a browser window, that was more evident. The webbrowser control is a better experience, but it does hide the fact that we really are going out to Twitter. I guess it still comes down to a matter of trust.
Mathias Lorenzen said,
Wrote on February 19, 2010 @ 5:41 am
As usual, you did a good job. However, to make it easier for your readers, include a solution or project file, so that it is all included in one click.
Shannon Whitley said,
Wrote on February 19, 2010 @ 8:12 am
Thanks @Mathias – I thought about doing that, but there are so many versions of Visual Studio that I try to keep it generic. Maybe I’ll change my mind now and include the project file.
davidbates said,
Wrote on February 19, 2010 @ 8:56 am
Great work. Good Clean code without overcommenting. Oh and it works too
I am with @mathias you don’t hurt anything by including a project file. I like it when I can just click and hit run
Shannon said,
Wrote on February 19, 2010 @ 10:44 am
If you have already stored the token and secret, then you can bypass all of the other code and just call the Twitter method from anywhere:
oAuthTwitter oAuth = new oAuthTwitter();
oAuth.Token = “{token}”;
oAuth.TokenSecret = “{secret}”;
url = “http://twitter.com/account/verify_credentials.xml”;
xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty);
webBrowser1.DocumentText = HttpUtility.HtmlEncode(xml);
Shannon said,
Wrote on February 19, 2010 @ 11:05 am
Added the project file to the zip file.
Mathias Lorenzen said,
Wrote on February 20, 2010 @ 3:51 am
For some reason, that didn’t work for me Shannon. It gives me an “Unauthorized” message when I do so.
Mathias Lorenzen said,
Wrote on February 20, 2010 @ 4:53 am
Maybe I am not storing the correct Token and Token Secret?
The Token I am storing for later use is the one I get from the AuthorizationLinkGet function, by grabbing the “oauth_token” value from the query string.
The Token Secret is grabbed from the oAuthTwitter.TokenSecret property after calling AccessTokenGet on a valid pincode. All of this works, but those tokens won’t use afterwards when used later.
Twitter xAuth with .NET | Shannon Whitley said,
Wrote on March 8, 2010 @ 3:38 pm
[...] modified the code from my previous PIN-based desktop application example. Make sure you keep track of the token and secret that Twitter returns and save that in a [...]
Twitter oAuth with .NET | Shannon Whitley said,
Wrote on March 8, 2010 @ 3:45 pm
[...] Twitter oAuth with .NET for the Desktop [...]
sujit said,
Wrote on April 7, 2010 @ 1:57 pm
hi… do I need to login again and again on Twitter ..
Shannon Whitley said,
Wrote on April 8, 2010 @ 9:53 pm
@sujit – Once you have the token and secret for the user, you just store them and reuse them. There’s no need to have the user login again unless they revoke the rights to your application.
Dheeraj said,
Wrote on April 27, 2010 @ 10:10 pm
I downloaded this code and opened it with Visual Studio 2008 press F5 . Its Giveing Error Error
1 Resource file “Properties\Resources.resx” cannot be found. oAuthDesktopPINSample
Please Guid me what should i do ???
Thanx in advance
Katy said,
Wrote on May 3, 2010 @ 8:35 pm
I am about to begin a book review column for an online newsletter, which I believe they must use Word Press to publish. I am trying to log into Word Press by clicking the ‘sign in with Twitter’ button. I allow TwitConnect access and it gives me a PIN but nowhere to enter the PIN? Am I missing something? Is there some sort of download I need that I don’t have? All the info I was given was to click on the ‘log into word press’ button on the newsletter site…
Shannon Whitley said,
Wrote on May 4, 2010 @ 9:49 am
@Katy – Check the blog setup on Twitter. Make sure the “Application Type” is set to “browser” (not “client”):
http://dev.twitter.com/apps/
jayapenTells (jayan k jose) said,
Wrote on May 13, 2010 @ 4:21 am
Twitter Comment
“Twitter oAuth with .NET for the Desktop” (Shannon Whitley) [link to post]
– Posted using Chat Catcher
Kaushal Parikh said,
Wrote on May 18, 2010 @ 12:37 am
Nice post.
I need your help. I have successfully verified the authorization from twitter.
Now i want to post a status update to the user’s twitter account but i want to use the same oauth_token and secret that was passed back by twitter.
I dont want the user to provide username or password.
How can i do it?
thanks
Shannon said,
Wrote on May 19, 2010 @ 2:18 pm
If you store the token and secret, you can reuse them for each call. Just create an oAuth object and set the token and secret to the previously stored objects before making the method call.
Andre Prefontaine said,
Wrote on June 8, 2010 @ 9:02 am
Thanks for the sample code Shannon. It was a great help in helping me get started with OAuth.
Note to @Dheeraj. Just start a new project and copy the source files into the project and you should be able to get the project to compile. It seems there are some resource files missing from the original project.
Note to others. I adapted this sample to get an OAuth token from Google. The encoding for parameters such as ‘scope’ fails because Google is expecting a character to escape special characters such as ‘:’ and ‘/’.
Again, thanks Shannon. The code was easy to follow and helped a lot.
Shannon Whitley said,
Wrote on June 8, 2010 @ 11:37 am
@Andre – Thanks for the comments and I appreciate the extra help for the other folks too.
Sunny said,
Wrote on July 8, 2010 @ 10:11 pm
Is there a way to authenticate my desktop client without using a web browser control? I am trying to build a simple windows form app that takes user name and password from the user to authenticate and then be able to access followers, tweets, and tweets posted by my followers.
Please advice.
Manoj Mohan said,
Wrote on July 8, 2010 @ 10:12 pm
Is there a way to authenticate my desktop client without using a web browser control? I am trying to build a simple windows form app that takes user name and password from the user to authenticate and then be able to access followers, tweets, and tweets posted by my followers.
I’m trying to use OAuthUtility class.
Please advice.
Manoj Mohan said,
Wrote on July 8, 2010 @ 10:13 pm
Is there a way to authenticate my desktop client without using a web browser control? I am trying to build a simple windows form app that takes user name and password from the user to authenticate and then be able to access followers, tweets, and tweets posted by my followers.
I’m trying to use OAuthUtility class.
Please advice.
Twitter oAuth in .Net without Web Login | PlanetB said,
Wrote on July 18, 2010 @ 7:08 pm
[...] came across this article about how to use Twitter oAuth with .Net (via Twitter API documentation) and used the extended [...]
Farrukh Javeid said,
Wrote on July 23, 2010 @ 7:45 am
Nice post…. I have found it after wasting a lot of hours…
I have a query though… I am having this exception in OAuth Class in the
public string GenerateSignatureBase()
I am having this exception due to value of ConsumerKey is Null…. Need help ASAP!
Jamie said,
Wrote on July 27, 2010 @ 5:50 am
@Farrukh, I think there’s a bug in Shannon’s code. In the oAuthTwitter class, I think I had to change the ConsumerKey Get property to return the _consumerKey variable. Something like that. I kinda forget, but yes, I was getting the same error.
Shannon Whitley said,
Wrote on July 27, 2010 @ 10:16 am
@Farrukh – Thank you. Please enter the consumer key and secret in the app.config file. It sounds like you haven’t updated app.config.
Thanks for the help @Jamie. There was a bug where you couldn’t set the consumer secret through code. I fixed that.
Jack said,
Wrote on August 10, 2010 @ 1:08 pm
I am still getting the hang of oauth but I was reading your posts and it sounds like in order to access a specific users profile; I need to send an authorization request to the twitter site get an authorized key which i need to use to then access a users profile or does the authorization request go to the user and if so how does the user receive this request and authorize it and how long does this take. It seems as if the authorization screen will pop up on my machine. Am I understanding this correctly?
Shannon Whitley said,
Wrote on August 12, 2010 @ 12:36 pm
Hi @Jack,
The entire process is normally initiated and completed on the user’s desktop. Your application requests the keys from Twitter based on user interaction. Authorization requests aren’t “sent” anywhere. The authorization request is presented to the user as part of a single process.
If you want to access a user’s profile, the user will need to be directed to a web page with the authorization process or the desktop app will need to include the authorization process.
It helps to know what type of application you’re building to point you in the right direction.
ZitronenMaus said,
Wrote on August 17, 2010 @ 1:03 am
Hi Shannon, very nice code and good understandable.
But I was very hopeless yesterday.
I tried to post a tweed. Found your sample code and it work well the first time. Further calls ever returned ’403 forbidden’. Hours later the same. It works once and than I got ‘forbidden’ from Http-Request.
I was nearly to rotate amok.
Today I had the enligthment.
Twitter blocked the post because my sample programm ever posted the same content. Adding GetDate and it worked well
Perhaps anybody has the same problem.
Thnaks to you Shannon
Varun Jain said,
Wrote on August 17, 2010 @ 3:00 am
Hello,
I’m developing a Desktop App for twitter. I want a feature to login multiple users at once and too without showing the login screen everytime (keep the session details). Can’t find a solution for this. Can anybody help me out for this. A working code would be really appreciated.
Thanks and Regards,
Varun Jain
Twitter oAuth with .NET for the Desktop Shannon Whitley said,
Wrote on August 22, 2010 @ 5:53 pm
[...] Twitter oAuth with .NET for the Desktop Shannon Whitley. // [...]
Implementing oauth twitter search in C# and JSON « Coder buddy said,
Wrote on August 28, 2010 @ 12:05 pm
[...] implementation, and a JSON parser for extracting results. For oauth implementation I’m using Twitter oAuth with .NET by Shannon whitley and for parsing the results JSON parser by Procurious and i have to say that this post is just a [...]
Sachin Bhalerao said,
Wrote on September 1, 2010 @ 4:19 am
I getting Exception for Consumer Key. what value should Consumer Key have in app.config file
Shannon Whitley said,
Wrote on September 2, 2010 @ 7:28 am
@Sachin Bhalerao – This is the API Key for your application fro http://dev.twitter.com/apps.
Twitti Family said,
Wrote on September 2, 2010 @ 11:19 am
Shannon,
Thanks for all you hard work on this desktop app! This code looks like just the help I need to tackle oAuth in my own app.
I’m using Visual Studio 2010; I opened your project, went through the upgrade wizard, set my consumer key and secret in app.config and attempted to run.
I got a popup from Windows saying that it had detected that the app did not run correctly. I tried running again and got a form with nothing in the window.
Any suggestions?
Twitti Family said,
Wrote on September 2, 2010 @ 12:29 pm
Nevermind! I went up to my Twitter app settings page and reset my consumer/key secret, then copy/pasted it into my app.config.
Bingo! The desktop app came up.
Thanks again!