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
data-text=”Twitter oAuth with .NET for the Desktop (Shannon Whitley)”
data-count=”vertical”
>Tweet
66 Responses to “Twitter oAuth with .NET for the Desktop”
Leave a Reply
7 Trackbacks
- Twitter xAuth with .NET | Shannon Whitley
[...] 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
[...] Twitter oAuth with .NET for the Desktop [...]
- jayapenTells (jayan k jose)
“Twitter oAuth with .NET for the Desktop” (Shannon Whitley) [link to post] - Twitter oAuth in .Net without Web Login | PlanetB
[...] came across this article about how to use Twitter oAuth with .Net (via Twitter API documentation) and used the extended [...]
- Twitter oAuth with .NET for the Desktop Shannon Whitley
[...] Twitter oAuth with .NET for the Desktop Shannon Whitley. // [...]
- Implementing oauth twitter search in C# and JSON « Coder buddy
[...] 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 [...]
- __sander (Sander Gerz)
Aanmeldingen voor dotNed meeting worden weer getweet. Even overstappen naar OAuth doe je zo [link to post]

Silverlight 4 with Out-of-Browser and the WebBrowser control can handle OAuth Callback. There’s a little bit of wiring up required but it’s great when the process requires no copy and paste from the user.
Here’s a working demo (requires an active account with http://www.etsy.com):
http://www.tools4etsy.com/developer/etsy-v2-api/samples/Silverlight/OAuth/default.aspx
If I can find some time, I’ll post an article on it.
G.
Hi Shannon,
I need to post a message to my twitter account using Twitter API from my .net web service application.
1. Whether I need a hosted web site to specify the callback URL? Is there any other alternative approach without specifying the online website callback URL?
Kindly provide your inputs/thoughts. Thanks in advance.
Regards
Gopi.R
@Gopi – This is the right post. Select “desktop” as your application type. The callback url is not used. Acting as the user, you can enter your userid and password, retrieve the PIN, and enter that PIN into your application to retrieve the oAuth token and secret. Once you have the oAuth token and secret, you can use them to send your automated message.
Thanks for the great code! This has saved me a ton of work. I was confused by what to save after the initial authorization, so to clarify for everyone else:
Do not save the initial token from this line:
lblToken.Text = HttpUtility.ParseQueryString(url.Query)["oauth_token"];
After you have retrieved the TokenSecret, save the oAuth.Token and the oAuth.TokenSecret (this token is different than the one above).
Otherwise you’ll see the 401 unauthorized errors.
Thanks again!
Thank you very much for posting this example- Very Clean. Hope you don’t mind, I have integrated it into my own code for personal use.
-k
Thanks for help with the clarification @theChrisKent.
@Keven Wolf – I’m happy you found the code useful. Feel free to use the code for personal or commercial use. There aren’t any restrictions.
Shannon, I am using oauth athentication. From my web application i want to pass user id and password,how will be the tweet post in that particular account? I have cosumer key, cosume secret, token key, token secret.
I want to avoid twitter login page for allow access.
Please suggest.
@Gopa If you have the Consumer Key, Secret, Token, and Token Secret, just create the oAuth object and set the objects properties to those values. Then you can make any call you like.
I got the pin and updated it i got an xml file. i find no secret tokens in that..the details in xml file are related to my account. But what to do next? I am new to programming so if my question is dumb one..sorry.
Hi,
I have to check when new tweet arrives as per Notification service in my .Net project. Is there any method to get return as new tweet arrives and then my process will run.
Thanks for the code. I’ve turned the two classes into a class library for use with my personal Twitter project. I’ve credited you in the help … just in case I ever release the program publicly. (Very unlikely at this stage, but you know how software can take over your life)
How do I get consumerKey and consumerSecret for Deskop application?
@Tony – https://dev.twitter.com/
how do i get the value of OAuthVersionKey, OAuthSignatureMethodKey, OAuthSignatureKey, OAuthTimestampKey, OAuthNonceKey?
please help
@keya – Those values will all be populated automatically if you follow the example. Remember to place your consumer key and secret in web.config.
I want to make a desktop application that auto-login twitter with username, password stored in database (multi-user), & post status update.
It should also follow the predefined twitter profile (mine).
hey …tell me what i fill in outUrl
and querySDtring and laos in ret…
thnc buddy
HI Shanon. I am trying to get the request_token from twitter in windows mobile. can you please let me know how to do this?? I tried twitterizer and it just hangs. I am not getting any response.I use VB.NET to build my app Please let me as soon as possible. thanks in advance.
Hi @Parthi, The oAuth process isn’t any different for mobile. You will need to rewrite the web calls since they are all asynchronous. I don’t have an example for you, but they are out there.
Hi Shannon,
Thanks for your great code. This saved my lot of time.
When we are running the app for the second time also, user will be prompted to enter the PIN for authorizing the app. How can we avoid it. How can we authorize the app on the first go?
Please comment.
Thanks,
Midhun
Hi Shannon,
Thanks once more for your great code.
I have pointed it out. We don’t need to redirect to login screen to force the user to enter username and passkey. We can use aouth_token and aouth_secret for future authorization of app.
Warm Regards,
Midhun.