Twitter Service Into UWP App Using UWP Community Toolkit

Introduction

This article is divided into three sections - The first two sections explain about the installation of the Twitter application and installation of UWP Service Tool kit , while the final part explains the integration of them into the application.

  1. Create Twitter application
  2. Install the UWP Service
  3. Integrate Twitter into main application

Create Twitter application

What is that Twitter application? Why is it required? By a simple example, we will understand the concept; for example,  some websites you can login with your Twitter account

Ex - http://www.c-sharpcorner.com/ you can login with your twitter account, for this concept implementation Csharpcorner requires communication with the twitter account, for this communication the Twitter application is required. Twitter application is a collection of APIs to interact with Twitter server.

Note: Twitter application is called access token.

The same concept if required in our application, first we have to create twitter application. Now we will see how to create the twitter application

Steps to Create Twitter application

  1. URL: https://apps.twitter.com/app/new

    Fill the Name & other information.


     
  2. Set the access permission.   

  3. Consumer key and Consumer secret will be automatically created.

     
    Twitter application is ready.

Install the UWP Toolkit

UWP Community Toolkit is a collection of helper functions, custom controls, and app services. It simplifies and demonstrates common developer tasks building UWP apps.

More info available here.

For our application to implement Twitter access, we don't have the need to write the code from scratch. UWP toolkit helper class is available. We just add the helper reference into our application & use helper function to interact with Twitter application.

Adding helper reference into our project.

Steps

  1. Create a new project (ex: Toolkit)
  2. Right click -> Manage NuGet Packages
  3. Search “Microsoft.Toolkit.Uwp”
  4. Select the “Microsoft.Toolkit.Uwp.Services” & Install.

UWP Tool kit has been installed in our application.

Integrated twitter into main application

Add the namespace and initialize the service (same project which we have created in the previous section).

 

 
  1. using Microsoft.Toolkit.Uwp.Services.Twitter;  
  2. TwitterService.Instance.Initialize(consumerKey, consumerSecurity, callbackUri);   
 

TwitterService.Instance.Initialize function is used to the request to the user for giving the permission to access the Twitter account (username and password must be entered) like the below image.

 

Once the permission has been given by the user, you can see this third party information into the user's Twitter account settings.

Go to -> https://twitter.com/settings/applications , permission has given to the Csharpcorner.

  
 
  1. 3.  To access twitter data, login to the twitter account for this call the   
  2. await TwitterService.Instance.LoginAsync() function   
Let's see the example to load the Twitter Information Display in our application.
 
XAML code 
 
  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.         <Grid.RowDefinitions>  
  3.             <RowDefinition Height="30*"/>  
  4.             <RowDefinition Height="70*"/>  
  5.         </Grid.RowDefinitions>  
  6.         <StackPanel x:Name="PanelUser" Margin="10" Orientation="Horizontal">  
  7.             <StackPanel>  
  8.                 <TextBlock Text="My Tweet Info"/>  
  9.                 <Image x:Name="ImgUser" Stretch="Fill" Source="{Binding ProfileImageUrl}"/>  
  10.                 <StackPanel Orientation="Vertical">  
  11.                     <TextBlock x:Name="TxtName" Text="{Binding Name}"/>  
  12.                     <TextBlock x:Name="TxtScreenName" Text="{Binding ScreenName}" />  
  13.                 </StackPanel>  
  14.             </StackPanel>  
  15.            <StackPanel Margin="10">  
  16.                 <TextBlock x:Name="TxtName1" Text="New Tweet"/>  
  17.                 <TextBox x:Name="NewTwitte" Text=" " Width="150"/>  
  18.                 <Button x:Name="BtnNewTwt" Content="Tweet" Margin="0,10,0,0" Click="BtnNewTwt_OnClick"/>  
  19.             </StackPanel>  
  20.         </StackPanel>  
  21.         <ListView x:Name="LstView" Grid.Row="1" Margin="10">  
  22.             <ListView.ItemTemplate>  
  23.                 <DataTemplate>  
  24.                     <Grid>  
  25.                         <Grid.ColumnDefinitions>  
  26.                             <ColumnDefinition Width="Auto"/>  
  27.                             <ColumnDefinition Width="Auto"/>  
  28.                         </Grid.ColumnDefinitions>  
  29.                         <StackPanel Grid.Column="1" Margin="20">  
  30.                             <TextBlock x:Name="TxtName" Text="{Binding TextInfo}"/>  
  31.                             <TextBlock x:Name="TxtScreenName" Text="{Binding CreateDate}"   
  32.                                        Foreground="Blue"  
  33.                                        FontWeight="Bold"/>  
  34.                         </StackPanel>  
  35.                     </Grid>  
  36.                 </DataTemplate>  
  37.             </ListView.ItemTemplate>  
  38.         </ListView>  
  39.     </Grid>  
Code
 
  1. public class TwitterInfo : INotifyPropertyChanged  
  2.     {  
  3.         public string TextInfo  
  4.         {  
  5.             get { return _textInfo; }  
  6.             set  
  7.             {  
  8.                 _textInfo = value;  
  9.                 OnPropertyChanged();  
  10.             }  
  11.         }  
  12.         private string _textInfo;  
  13.   
  14.         public string ImgSource  
  15.         {  
  16.             get { return _imgSource; }  
  17.             set  
  18.             {  
  19.                 _imgSource = value;  
  20.                 OnPropertyChanged();  
  21.             }  
  22.         }  
  23.         private string _imgSource;  
  24.   
  25.         private string _createDate;  
  26.   
  27.         public string CreateDate  
  28.         {  
  29.             get { return _createDate; }  
  30.             set  
  31.             {  
  32.                 _createDate = value;  
  33.                 OnPropertyChanged();  
  34.             }  
  35.         }  
  36.   
  37.   
  38.         public event PropertyChangedEventHandler PropertyChanged;  
  39.   
  40.         [NotifyPropertyChangedInvocator]  
  41.         protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)  
  42.         {  
  43.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
  44.         }  
  45.     }  
  46.   
  47. public ObservableCollection<TwitterInfo> TwitterInfoCollection = new ObservableCollection<TwitterInfo>();  
  48.   
  49. private async void LoginTwitter()  
  50.         {  
  51.             TwitterService.Instance.Initialize(consumerKey, consumerSecurity, callbackUri);  
  52.   
  53.             if (!await TwitterService.Instance.LoginAsync()) return;  
  54.   
  55.             var user = await TwitterService.Instance.GetUserAsync();  
  56.             await LoadTwitter(user);  
  57.             PanelUser.DataContext = user;  
  58.         }  
  59.   
  60.         private async Task LoadTwitter(TwitterUser user)  
  61.         {  
  62.             var lstTwitter = await TwitterService.Instance.GetUserTimeLineAsync(user.ScreenName);  
  63.             TwitterInfoCollection = new ObservableCollection<TwitterInfo>();  
  64.   
  65.             foreach (var tweet in lstTwitter)  
  66.             {  
  67.                 var twitterInfo = new TwitterInfo  
  68.                 {  
  69.                     ImgSource = tweet.User.ProfileImageUrl,  
  70.                     TextInfo = tweet.Text,  
  71.                     CreateDate = tweet.CreatedAt  
  72.                 };  
  73.                 TwitterInfoCollection.Add(twitterInfo);  
  74.             }  
  75.   
  76.             LstView.ItemsSource = TwitterInfoCollection;  
  77.         }  
  78.   
  79.         private async void BtnNewTwt_OnClick(object sender, RoutedEventArgs e)  
  80.         {  
  81.             await TwitterService.Instance.TweetStatusAsync(NewTwitte.Text);  
  82.             var user = await TwitterService.Instance.GetUserAsync();  
  83.             await LoadTwitter(user);  
  84.         }  
Final output

 

Up Next
    Ebook Download
    View all
    Learn
    View all