Twitter: Posting Tweets using Hammock
m (Croozeus - Formatted) |
m (Croozeus - - →Prerequisites) |
||
| Line 30: | Line 30: | ||
==Prerequisites== | ==Prerequisites== | ||
| − | + | User should be already logged in Twitter to use the code shown in this article. You may refer [[Twitter: OAuth 1.0 Authentication using Hammock|this]] article to know about how to login to Twitter using Hammock. | |
| − | + | ||
==Code Behind== | ==Code Behind== | ||
Revision as of 10:42, 1 February 2013
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.
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Prerequisites
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 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.

