(Difference between revisions)

Twitter: Posting Tweets using Hammock

From Nokia Developer Wiki
Jump to: navigation, search
Vaishali Rawat (Talk | contribs)
(Vaishali Rawat - - Changes in UI as compared to previous version of this article)
Vaishali Rawat (Talk | contribs)
(Vaishali Rawat - - Prerequisites)
Line 26: Line 26:
* User should be already logged in in Twitter.
* User should be already logged in Twitter.
{{Note|You may refer [http://www.developer.nokia.com/Community/Wiki/Twitter:_OAuth_1.0_Authentication_using_Hammock this] article to know about how to login to Twitter using Hammock.}}
{{Note|You may refer [http://www.developer.nokia.com/Community/Wiki/Twitter:_OAuth_1.0_Authentication_using_Hammock this] article to know about how to login to Twitter using Hammock.}}

Revision as of 10:56, 22 January 2013

This article will help you in posting your tweets/ updating your timeline status in Twitter using Hammock library

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, 7.5
Windows Phone 8
Windows Phone 7.5
Created: Vaishali Rawat (22 Jan 2013)
Last edited: Vaishali Rawat (22 Jan 2013)



  • User should be already logged in Twitter.

Note.pngNote: You may refer this article to know about how to login to Twitter using Hammock.

Code Behind

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 = "http://api.twitter.com",
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));

Via above code, 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 = "";

Above, 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.


176 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.