Click here to Skip to main content
13,596,321 members
Click here to Skip to main content
Add your own
alternative version

Stats

25.1K views
965 downloads
47 bookmarked
Posted 30 Jan 2018
Licenced CPOL

Building a C# WPF Twitter Reader with REST API

, 17 May 2018
Rate this:
Please Sign up or sign in to vote.
Create a C# WPF app to read Twitter tweets and retweets via REST API with grouping related users / friends by categories

Introduction

(A quick note: if you downloaded this app's souce code before, it's been updated.)

As a Twitter user, you may follow a number of interesting people and organizations to read their tweets and retweets. It can be quite helpful if you group users in categories and focus on certain types of users at a particular time, cutting down noises from the rest. If you have multiple Twitter login accounts, you may want to switch between the accounts quickly and easily.

Background

Twitter provides a development platform for developers. You can use REST API + OAuth to access tweets and retweets: https://developer.twitter.com

With my app, you can read tweets and retweets with a pre-defined Twitter login I created. You can also register a Twitter app at https://apps.twitter.com with your own Twitter account and use the credentials to see your Twitter login in action! See TwitterCredentialsSetup.txt in download.

From a technical point of view, you can review or learn how the following techniques are being applied:

  • C# 6 / WPF / TreeView / TabControl / ListView / ContextMenu / MVVM / ...
  • REST API / OAuth / Json Serialization / HttpClient / HttpClientHandler / ...

Using the Code

When launched, the app looks like this:

The testing login TweetTesterNET (screen name) in the dropdown list follows 7 users in 3 groups. The Generate Groups button is for a one-time function to create the 3 pre-defined groups (already done in the screenshot). If you have multiple Twitter login accounts, you can prepare them in json files to be loaded into the dropdown list.

As shown, .NET Team (@dotnet) is the selected user, and its tweets and retweets are displayed in a TabControl on the right.

There are 2 projects. TwitterAccess is a class library containing 'Twitter APIs'. TwitterReader is the WPF app.

TweetEntity represents a tweet, containing Id, FullText, CreatedBy, etc. Entities are serialized and deserialized Newtonsoft.Json for REST API.

public class TweetEntity
{                
    [JsonProperty("id")]
    public long Id { get; set; }        
        
    [JsonProperty("full_text")]
    public string FullText { get; set; }

    [JsonProperty("user")]
    public UserEntity CreatedBy { get; set; }
...
}

To get tweets and retweets for a user, use this method:

public List<TweetEntity> GetUserTweetList(long userId, int count, bool includeRetweet = false)
{
    var twitterQuery = TwitterQuery.Create(HttpMethod.Get, TwitterConstants.UserTweetsUrl);
    twitterQuery.AddParameter("user_id", userId);
    twitterQuery.AddParameter("include_rts", includeRetweet);
    twitterQuery.AddParameter("exclude_replies", false);
    twitterQuery.AddParameter("contributor_details", false);
    twitterQuery.AddParameter("count", count);
    twitterQuery.AddParameter("trim_user", false);
    twitterQuery.AddParameter("include_entities", true);
    twitterQuery.AddParameter("tweet_mode", "extended");
 
    string result = ExecuteQuery(twitterQuery);
    var tweetList = JsonHelper.DeserializeToClass<List<TweetEntity>>(result);
    return tweetList ?? new List<TweetEntity>();
}

History

  • Updated the code on 3/3/2018 to support a Twitter account with more than 100 friends
  • Updated the code to improve UI on 2/18/2018
  • Updated the code and article on 2/4/2018 for multiple Twitter login support and UI changes

I used Visual Studio 2015 and C# 6. You can download the source code and read TwitterCredentialsSetup.txt for instructions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Peter Sun (247)
Software Developer (Senior)
United States United States
I have been a full stack Windows developer for 20+ years and is based in Orange County, California. Currently, I am focusing on C#, WPF, Azure, .NET Core, and Xamarin.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
PraiseGreat WPF example Pin
Ira M Nye23-Feb-18 9:11
memberIra M Nye23-Feb-18 9:11 
GeneralRe: Great WPF example Pin
Peter Sun (247)23-Feb-18 19:28
memberPeter Sun (247)23-Feb-18 19:28 
QuestionMessage Removed Pin
18-Feb-18 18:11
memberMember 1367902518-Feb-18 18:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 18 May 2018
Article Copyright 2018 by Peter Sun (247)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid