×
Namespaces

Variants
Actions
Revision as of 10:13, 30 January 2013 by Vaishali Rawat (Talk | contribs)

Twitter: Posting Tweets using Hammock

From Nokia Developer Wiki
Jump to: navigation, search

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

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

Contents

Prerequisites

  • 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)
{
MessageBox.Show(AppSettings.TWEET_POSTED_SUCCESSFULLY);
}
else if (response.StatusCode == HttpStatusCode.Forbidden)
{
MessageBox.Show(AppSettings.TWEET_POST_ERR_UPDATE_LIMIT);
}
else
{
MessageBox.Show(AppSettings.TWEET_POST_ERR_FAILED);
}
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">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
 
<phone:WebBrowser Grid.Row="0" Margin="-6,3,0,1" Name="loginBrowserControl" Visibility="Collapsed"
Navigated="loginBrowserControl_Navigated" Navigating="loginBrowserControl_Navigating"
IsScriptEnabled="True"/>
 
<Grid x:Name="TweetPanel" Grid.Row="0" Visibility="Collapsed">
<Grid.RowDefinitions>
<RowDefinition Height=".15*"/>
<RowDefinition Height=".10*"/>
<RowDefinition Height=".60*"/>
<RowDefinition Height=".15*"/>
</Grid.RowDefinitions>
 
<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"/>
 
</Grid>
</Grid>

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.

References

168 page views in the last 30 days.
×