Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Twitter: Posting Tweets using Hammock

From Wiki
Jump to: navigation, search

This article will help you in posting your tweets/ updating your timeline status in Twitter using Hammock library.. The screenshots of this project (assuming the user is logged in already) are shown below.

LoggedIn.png TextEntered.png

WP Metro Icon Web.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleTested with
SDK: Windows Phone 7.1
Devices(s): Nokia Lumia 800
Platform(s): Windows Phone 7.5
Windows Phone 8
Windows Phone 7.5
Created: Vaishali Rawat (22 Jan 2013)
Last edited: pavan.pareta (08 Jul 2013)



User should be already logged in Twitter to use the code shown in this article. You may refer this article to know about how to login to Twitter using Hammock.

Code Snippet

The following method needs to be implemented to tweet posts in Twitter.

private void btnPostTweet_Click(object sender, RoutedEventArgs e)
var credentials = new OAuthCredentials
Type = OAuthType.ProtectedResource,
SignatureMethod = OAuthSignatureMethod.HmacSha1,
ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
ConsumerKey = AppSettings.consumerKey,
ConsumerSecret = AppSettings.consumerKeySecret,
Token = this.accessToken,
TokenSecret = this.accessTokenSecret,
Version = "1.0"
var restClient = new RestClient
Authority = "",
HasElevatedPermissions = true
var restRequest = new RestRequest
Credentials = credentials,
Path = "/1/statuses/update.json",
Method = WebMethod.Post
restRequest.AddParameter("status", txtTweetContent.Text);
restClient.BeginRequest(restRequest, new RestCallback(PostTweetRequestCallback));

Using the above snippet, we've created an instance of oAuthCredentials, RestClient & RestRequest classes. All these classes are part of Hammock library. The credentials object is binded to the RestRequest's object. The client request is fired and a callback is declared for catching the event.

  • The callback event handler is defined as:
private void PostTweetRequestCallback(RestRequest request, RestResponse response, object obj)
Deployment.Current.Dispatcher.BeginInvoke(() =>
if (response.StatusCode == HttpStatusCode.OK)
else if (response.StatusCode == HttpStatusCode.Forbidden)
txtTweetContent.Text = "";

As shown in the above snippet, we are checking the response type and on it's basis, showing the proper prompt message to the user.

Changes with Previous version

If you are following this article to get logged in in Twitter using Hammock and upgrading it with posting tweets code, then there are some changes in the UI.

Changes in UI as compared to previous version of this article

  • The MainPage.xaml file has the following contents now.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<RowDefinition Height="*"/>
<phone:WebBrowser Grid.Row="0" Margin="-6,3,0,1" Name="loginBrowserControl" Visibility="Collapsed"
Navigated="loginBrowserControl_Navigated" Navigating="loginBrowserControl_Navigating"
<Grid x:Name="TweetPanel" Grid.Row="0" Visibility="Collapsed">
<RowDefinition Height=".15*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".60*"/>
<RowDefinition Height=".15*"/>
<TextBlock Grid.Row="0" x:Name="txtUserName" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="26" FontFamily="Segoe WP Bold" Foreground="Red"/>
<TextBlock Grid.Row="1" Margin="10" VerticalAlignment="Center" HorizontalAlignment="Left" Text="Enter your Tweet" FontSize="24" FontFamily="Segoe WP Bold"/>
<TextBox Grid.Row="2" x:Name="txtTweetContent" Margin="20,-10,20,100" TextWrapping="Wrap" Text="" FontSize="24" FontFamily="Segoe WP Bold" />
<Button Grid.Row="3" x:Name="btnPostTweet" Content="Post Tweet" HorizontalAlignment="Center" Click="btnPostTweet_Click"/>

Build and Run

  • You may directly run the source code attached with this article to test it or embed the above defined methods in your own project.


This page was last modified on 8 July 2013, at 08:38.
49 page views in the last 30 days.