Just like with Facebook, sharing statuses on Twitter from Windows Phone app can be simply accomplished using ShareStatusTask launcher. A more complicated scenario is when you want to share a photo on Twitter. In order to do that, you first need to authenticate the user from your app to allow your app to communicate with Twitter and then to upload photos. This article will show you how to do the authentication part.

Before doing the authentication, you need to make sure you register your app on:


Login with your Twitter account. Create a new application if you haven’t already done so.


After that, you’ll get Consumer Key and Consumer Secret keys that you’ll use from your Windows Phone app. Next, go to your Windows Phone app or create a new one. Create a class called TwitterSettings.cs with the following static properties:

Hardcode your consumer key, consumer secret and the callback URI (you defined that when registering your app on dev.twitter.com).

The next step is to add a library called TweetSharp to your project. In my opinion, that’s the best open source library for communicating with Twitter from C#.


The fastest way to get it is through Nuget.

Now create a Windows Phone page for authentication. Let’s take a look at how does the process of authentication go. You will use something called a PIN-based authentication.


This means that you’ll have to ask the user to login with his Twitter account using Browser control (or WebBrowser launcher) after which he’ll get a PIN. The user will then copy that PIN somewhere inside your app after which you’ll get information you need to do stuff on user’s Twitter. This information includes Access Token, Access Token Secret, UserID and Screen Name. You should create a class for holding that information (something similar to what I showed you in in my Facebook authentication article). Call it TwitterAccess.cs

Now, define the rest of the page for authentication, something like this:

In the code behind, define the following:

On navigating to the page, get Twitter token:

You create the TwitterService object with the ConsumerKey and ConsumerKeySecret, the hardcoded values from TwitterSettings class. Then you define the callback method which you’ll use for getting the authentication URI and navigate the browser to it. Then you call the GetRequestToken method with parameters “oob” and that callback method to get the request token. Parameter “oob” means that we are using the PIN-based authentication method. The callback method gets the necesssary authentication URI and then opens that URI in the browser control.

The browser will display a Twitter website login with your app info, telling the user that your app wants access to their account. If they login, they’ll get back a PIN. After they copy that PIN to your textbox and press the login button, then you do the following:

The most important part is defining the callback method for getting the access token, CallBackVerifiedResponse, and call the GetAccessToken method with parameters: request token that you got earlier, the PIN that the user copy/pasted to your textbox and the name of the callback method. The CallBackVerifiedResponse method checks to see if the token was actually acquired (if the PIN was entered correctly or not) and then serializes information to isolated storage.

If you don’t know how to write the serialization method SaveSetting, see here in one of my last articles regarding the Facebook authentication:


That’s all you need to do. Now you have the access token and access token secret which you’ll use for anything you need to do with Twitter from your app!

If you want to see how it’s done in an app, go check my app Photo Light!