TwitterRt - Tweeting from within your Windows 8 App

Sign in to queue

Description

Today's Modern UI Monday post is a library by John Michael Hauck, with source too, that makes tweeting from your Windows 8 app almost too simple and easy. And as an added bonus, you not only get the library, but a tutorial on how to use Twitter's API too.

TwitterRt - Tweet from Windows Metro Apps

My goal is to make tweeting from a C# Metro application as easy as possible. To that end, I created a library called TwitterRt.

Install TwitterRt from NuGet.org and add to your existing Metro application:

  1. Install the NuGet Package Manager if you have not already.
  2. Open your Metro application in Visual Studio.
  3. Select Manage NuGet Packages from the Project menu.
  4. Click Online. Search for TwitterRt. Click Install.

To add the ability to tweet from your application, let's say its called MyTwitApp:

  1. Create your own personal twitter account if you don't have one, at twitter.com.
    Now log out of twitter.
  2. Create your application's own twitter account,
    preferably with the name MyTwitApp (or whatever your app is called), at twitter.com.
    1. Navigate to dev.twitter.com,
      select "Create an app",
      call it "MyTwitApp" (or whatever your app is called)
    2. Make sure to specify (*) read, write and access direct messages.
    3. Make sure to specify a callback url of http://MyTwitApp.com
      (or whatever your app is called), even if it is not a real web site.
  3. From the Details page of your dev.twitter.com's MyTwitApp (or whatever your app is called):
    1. Ensure the "Access level" is "Read, write, and direct messages"
    2. Remember the Consumer key, Consumer secret, and Callback URL for step 5.
  4. There is an option on the Details page called "Create my access token".
    Found it? Great! Ignore it.
  5. From your application:
    1. Construct a TwitterRt class, passing the consumer key, consumer secret and callback url found in step 3 above.
    2. Add a call to await GainAccessToTwitter()
      (Yeah, you need to make your method "async")
    3. Run your app so that it makes the call to GainAccessToTwitter()
    4. When confronted with the twitter signin screen that magically appears from within your application, log in to your personal twitter account and click the "authorize button" (you might need to scroll down)
  6. From a web browser:
    1. Log in to twitter using your personal account.
    2. Navigate to https://twitter.com/settings/applications to see if your application is listed.
    3. If it is listed, great! If not, I dunno what to tell you.
  7. From your application, call await Tweet("test")
    (Yeah, you need to make your method "async")
  8. Go back to the web browser and see if you tweeted "test".
    If it is there, great! If not, look at the value of Status.

...

<Page
   x:Class="TwitterRtDemoApp.MainPage"
   IsTabStop="false"
   xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local="using:TwitterRtDemoApp"
   xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable="d">
    <Grid
       Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel
           VerticalAlignment="Center"
           HorizontalAlignment="Center">
            <Button
               Click="OnAuthorize">Authorize Twitter</Button>
            <Button
               Click="OnTweet">Tweet Now</Button>
            <TextBlock
               x:Name="_statusTextBlock" />
        </StackPanel>
    </Grid>
</Page>

using System;
using System.Threading.Tasks;
using TwitterRtLibrary;
using Windows.UI.Xaml.Controls;

namespace TwitterRtDemoApp
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            TwitterRt = new TwitterRt("consumerKey goes here", "consumerSecret goes here", "callbackUrl goes here");
        }

        private async void OnAuthorize(object sender, Windows.UI.Xaml.RoutedEventArgs args)
        {
            await TwitterRt.GainAccessToTwitter();
            _statusTextBlock.Text = TwitterRt.Status;
        }

        private async void OnTweet(object sender, Windows.UI.Xaml.RoutedEventArgs args)
        {
            await TwitterRt.UpdateStatus("Hello From TwitterRt " + DateTime.Now);
            _statusTextBlock.Text = TwitterRt.Status;
        }

        public TwitterRt TwitterRt { get; private set; }
    }
}

More Information

Latest TwitterRt binaries reside here: http://nuget.org/packages/twitterrt/

Latest TwitterRt source resides here: http://twitterrt.codeplex.com/

Blog:

What I really liked was how John not only talks about his library and how to use it, but how it actually works, the magic behind the library. It provides a great tutorial for communicating with Twitter. Not that you need this anymore now that you have this library, but it still provides some great technical details and we all love knowing how out libraries work, right?

TwitterRT - The Design

This post describes the inner workings of TwitterRT and how it fulfills the Twitter OAUTH API. You do not need to read this if you are just using TwitterRT and do not really care how it works.

There are three major parts to issuing a tweet on behalf of your user:

  • Register your application with Twitter.
    This is a bit involved and not explained in this post.
    Instead, first read Posting To Twitter from Metro in C#.
  • Signing in to Twitter from your application.
    This is more complicated and is described in 3 steps below.
  • Posting Tweets.
    This is pretty easy and described at the end of this post.

Make sure you also check out the other related articles;

The Discussion

Add Your 2 Cents