Using the Twitter API

In this recipe, let's build a Twitter client app using the Twitter API. This is similar to the previous RSS reader recipe. Here, instead of RSS format you will receive the custom XML format related to the Twitter username or screen name.

Getting ready

First study the Twitter API resource page at:

https://dev.twitter.com/docs/api

There are many categories of API available such as Timelines, Tweets, Search, and so on. We will be using statuses/user_timeline for a given user in this recipe.

How to do it...

In the next steps, we will create a client application, which will have a textbox to search for a name and return the latest updates for that username.

  1. Open a new project and create a new Phone 7 Application with the name Recipe2_TwiitterClient.
  2. Right-click on the project folder and add a new class to the TwitterFeed.cs file. Open the file and add the new class to store the Twitter feed information. We will have one string variable called Content:
    public class TwitterFeed
    {
    public string Content { get; set; }
    }
    
  3. Open the MainPage.xaml file and change the ApplicationTitle and PageTitle:
    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
    <TextBlock x:Name="ApplicationTitle" Text="CH6-Recipes" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="Twitter Client" Margin="9,- 7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    
  4. Add the Search TextBlock and a TextBox following it:
    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBox Height="72" HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text="BillGates" VerticalAlignment="Top" Width="460" />
    <TextBlock Height="30" HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="Search:" VerticalAlignment="Top" />
    <Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
    </Grid>
    
  5. Now add a ListBox along with a TextBlock inside the Grid control "ContentPanel2":
    <Grid x:Name="ContentPanel2" Margin="12,217,12,-13" Grid.Row="1">
    <ListBox x:Name ="lstItems" Margin="0,6,0,0">
    <ListBox.ItemTemplate>
    <DataTemplate>
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition Height="20"/>
    </Grid.RowDefinitions>
    <StackPanel VerticalAlignment="Top">
    <TextBlock Grid.Row="0" Text="{Binding Content}"/>
    <TextBlock Grid.Row="1" />
    </StackPanel>
    </Grid>
    </DataTemplate>
    </ListBox.ItemTemplate>
    </ListBox>
    </Grid>
    
  6. Open the MainPage.xaml.cs file and add the following code using the WebClient class and then DownloadStringCompletedEventHandler:
    public partial class MainPage : PhoneApplicationPage
    {
    // Constructor
    WebClient remoteXml;
    public MainPage()
    {
    InitializeComponent();
    remoteXml = new WebClient();
    remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler (remoteXml_DownloadStringCompleted);
    }
    private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
    if (e.Error != null)
    return;
    XDocument xdoc = XDocument.Parse(e.Result);
    List<TwitterFeed> tweets;
    tweets = (from entry in xdoc.Descendants("status")
    select new TwitterFeed()
    {
    Content = entry.Element("text").Value,
    }).ToList();
    lstItems.ItemsSource = tweets;
    }
    
  7. Add the button click event for Go. In this method, we will hard-code the Twitter API URL for user timeline. In the query string, we will append the Twitter username, which we get from the textbox. Then, we will call DownloadStringAsync to navigate to the URL passed:
    private void button1_Click(object sender, RoutedEventArgs e)
    {
    string txtSearch = textBox1.Text;
    string txtUri = "http://api.twitter.com//1/statuses/ user_timeline.xml?screen_name=" + txtSearch;
    Uri uri = new Uri(txtUri, UriKind.Absolute);
    remoteXml.DownloadStringAsync(uri);
    }
    
  8. Press F5 and you should get the search textbox with the Go button. Type in the Twitter username or screen name. When you click on Go, you should see all the User Tweets:
    How to do it...

How it works...

In the preceding recipe, you got the standard RSS formatted XML, which is similar across all RSS feeds. Here twitter provides its own set of APIs to provide different XML content. One such API is user timeline, which takes the input screen_name and returns all the tweet updates for that user.

We used the WebClient class to navigate and download the results of the API call. In the DownloadStringCompleted event method, we used LINQ to XML. Here we parsed the returned XML file into the XDocument object and then queried the XML document for text nodes.

There's more...

In this recipe, you understood how simple it is to call any REST API, download the result, and save it in local objects. You can refer to the Twitter API reference document to build a robust client app.

See also

Check the first recipe titled Consuming RSS Feeds. Also check the following recipe to learn how to build your own REST service.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset