(Update : For trying out code posted on this blog post, Kindly use the official/ locked emulator . The unlocked emulator images have a known issue with HTTPS )

This is the second post in the series of posts explaining how you can build your own twitter application on Windows Phone 7

Post(s) in this series:

Part 1: Understanding oAuth

As explained in part one, the hard part of writing a twitter application is, to figure out/ understand the authentication/ authorization mechanism. Once You have done that, you can pretty much write an app for any service that has oauth as its authentication/authorization mechanism.

Now that we have obtained all the necessary authorization token(s), we can now access the protected resources on the user’s behalf.

How to Post a Tweet on a User’s behalf?

Using the access token and our app’s consumer secret , we need to make a POST request to twitter’s API to post a tweet from our app.

Using Hammock’s REST Library, You would do something like this :

if (txtBoxNewTweet.Text.Trim().Length == 0) { return; } var credentials = new OAuthCredentials { Type = OAuthType.ProtectedResource, SignatureMethod = OAuthSignatureMethod.HmacSha1, ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader, ConsumerKey = TwitterSettings.consumerKey, ConsumerSecret = TwitterSettings.consumerKeySecret, Token = this.accessToken, TokenSecret = this.accessTokenSecret, Version = "1.0" }; var restClient = new RestClient { Authority = TwitterSettings.StatusUpdateUrl, HasElevatedPermissions = true, Credentials = credentials, Method = WebMethod.Post }; restClient.AddHeader("Content-Type", "application/x-www-form-urlencoded"); // Create a Rest Request and fire it var restRequest = new RestRequest { Path = "1/statuses/update.xml?status=" + txtBoxNewTweet.Text }; var ByteData = Encoding.UTF8.GetBytes(txtBoxNewTweet.Text); restRequest.AddPostContent(ByteData); restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));

Now that you are able to make posts, you need a way to see the posts you have made on the user’s twitter account!

Well For looking at status updates, you don’t need authorization. You can directly give a call to the twitter api and you will get your status updates. ( This sample code is as demonstrated on Scott Gu’s blog )

private void GetUserTimeLine() { WebClient wcTwitterTimeline = new WebClient(); wcTwitterTimeline.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcTwitterTimeline_DownloadStringCompleted); wcTwitterTimeline.DownloadStringAsync(new System.Uri("http://api.twitter.com/1/statuses/public_timeline.xml?screen_name=" + userScreenName)); } void wcTwitterTimeline_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { return; } XElement Tweets = XElement.Parse(e.Result); listboxMyTimeline.ItemsSource = from tweet in Tweets.Descendants("status") select new TwitterItem { UserName = tweet.Element("user").Element("screen_name").Value, Tweet = tweet.Element("text").Value, ImageSource = tweet.Element("user").Element("profile_image_url").Value }; Dispatcher.BeginInvoke(() => { listboxMyTimeline.Visibility = Visibility.Visible; txtBoxNewTweet.Visibility = Visibility.Visible; btnPostTweet.Visibility = Visibility.Visible; }); }

With this, you are pretty much able to make a basic twitter app, that allows you to post tweets.

Using the Twitter api (Found here) You can create a full fledged twitter app.

As promised, I am uploading the source code for the app. You can download the source here.

(UPDATE : I have updated the code snippet, so that the app now runs on the public beta of the tools , Thanks Don for pointing this out )

Once again, Thanks for your support. With the final SDK Bits coming out this September 16th and with WP7 hitting the RTM status (Congratulations to the Windows Phone team @MSFT, Will be queuing up the store when, it hits retail ), The next couple of weeks seem to be really really interesting. Stay tuned for more exciting WP7 stuff

18.507673 73.800734