×
Namespaces

Variants
Actions
(Difference between revisions)

Twitter: Posting Tweets using Hammock

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix metadata)
pavan.pareta (Talk | contribs)
(Pavan.pareta - - Code Snippet)
 
(15 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Windows Phone 8]][[Category:Windows Phone 7.5]][[Category:Silverlight]]
+
[[Category:Web Services on Windows Phone]][[Category:Windows Phone 8]][[Category:Windows Phone 7.5]][[Category:XAML]][[Category:Code Examples]]
{{Abstract|This article will help you in posting your tweets/ updating your timeline status in Twitter using [http://nuget.org/packages/Hammock Hammock library]}}
+
{{Abstract|This article will help you in posting your tweets/ updating your timeline status in Twitter using [http://nuget.org/packages/Hammock Hammock library.]}}. The screenshots of this project (assuming the user is logged in already) are shown below.
 +
<br><br>
 +
[[Image:LoggedIn.png|240x320px]]
 +
[[Image:TextEntered.png|240x320px]]
 +
 
  
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
Line 7: Line 11:
 
|devices= Nokia Lumia 800<!-- Devices tested against - e.g. ''devices=Nokia Lumia 800'') -->
 
|devices= Nokia Lumia 800<!-- Devices tested against - e.g. ''devices=Nokia Lumia 800'') -->
 
|sdk= Windows Phone 7.1 <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 
|sdk= Windows Phone 7.1 <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
|platform= Windows Phone 7, 7.5
+
|platform= Windows Phone 7.5
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
Line 26: Line 30:
  
 
==Prerequisites==
 
==Prerequisites==
* User should be already logged in Twitter.
+
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.
{{Note|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 Snippet==
 
The following method needs to be implemented to tweet posts in Twitter.
 
The following method needs to be implemented to tweet posts in Twitter.
 
<code csharp>
 
<code csharp>
Line 64: Line 67:
 
</code>
 
</code>
  
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.
+
Using the above snippet, we've created an instance of {{Icode|oAuthCredentials}}, {{Icode|RestClient}} & {{Icode|RestRequest}} classes. All these classes are part of Hammock library.  The {{Icode|credentials}} object is binded to the {{Icode|RestRequest}}'s object. The client request is fired and a callback is declared for catching the event.
  
 
*The callback event handler is defined as:
 
*The callback event handler is defined as:
Line 89: Line 92:
 
</code>
 
</code>
  
Above, we are checking the response type and on it's basis, showing the proper prompt message to the user.
+
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 ==
 
==Changes with Previous version ==
If you are following [[Twitter: OAuth 1.0 Authentication using Hammock|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.
+
If you are following [[Twitter: OAuth 1.0 Authentication using Hammock|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==
 
== Changes in UI as compared to previous version of this article==
* The mainPage.xaml file has the following contents now.
+
* The '''MainPage.xaml''' file has the following contents now.
  
 
<code xml>
 
<code xml>
Line 132: Line 135:
 
*http://oauth.net/documentation/getting-started/
 
*http://oauth.net/documentation/getting-started/
 
*https://dev.twitter.com/docs/auth/oauth
 
*https://dev.twitter.com/docs/auth/oauth
[[Category:Code Examples]]
 

Latest revision as of 11:38, 8 July 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.

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
Compatibility
Platform(s): Windows Phone 7.5
Windows Phone 8
Windows Phone 7.5
Article
Created: Vaishali Rawat (22 Jan 2013)
Last edited: pavan.pareta (08 Jul 2013)

Contents

[edit] 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.

[edit] 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 = "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));
}

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)
{
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 = "";
});
}

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.

[edit] 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.

[edit] 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>

[edit] 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.

[edit] References

This page was last modified on 8 July 2013, at 11:38.
182 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.

×