Introduction
On 11/25/2019, I added TwitterReaderV2NETCore_11252019.zip, which supports .NET Core 3.0. Major information to use the source code, and changes:
- Download .NET Core 3.0 SDK to compile with Visual Studio 2019.
- After Publish in VS 2019, a single executable can be found at TwitterReaderV2NETCore_11252019\TwitterReaderV2NETCore\bin\Release\netcoreapp3.0\publish
- Newtonsoft.Json is removed. The built-in System.Text.Json is used.
Please leave me messages below if you have questions about the .NET Core version.
As of 2019-03-05, a new implementation is attached for you to download: TwitterReaderV2_03052019.zip. This includes a new UI (see below), and the ability to save tweets in a Microsoft Azure Service and an Azure website to view saved tweets. The Azure part is runtime only and code explanation is out of the scope of this article.
The article's original descriptions continue as follows.
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 with a new implementation: TwitterReaderV2_03052019.zip
- 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.