This is the archived version of Roland Weigelt's weblog that ran from 2003 to 2023 at weblogs.asp.net

How to Search Twitter from Your UWP App

Yesterday, the UWP Community Toolkit was released, “a new project that enables the developer community to collaborate and contribute new capabilities on top of the SDK.”. The toolkit “includes new capabilities (helper functions, custom controls and app services) that simplify or demonstrate common developer tasks”.

UWP-community-toolkit-overview

One thing that caught my eye was the support for social media like Twitter and Facebook. I had read the docs at https://dev.twitter.com/ and I had a twitter app configured at https://apps.twitter.com/ some months ago, but I never got around to actually choose a Twitter access library and use it.

But the few lines of sample code in the announcement (how to post a tweet) looked so simple I couldn’t resist trying to perform a Twitter search.

  • Follow the “Getting Started” instructions in the article. (I chose the packages Microsoft.Toolkit.Uwp and Microsoft.Toolkit.Uwp.Services)
  • Put a button on the form, create a click handler with the following code:
    private async void HandleButtonClick(object sender, RoutedEventArgs e)
    {
    	TwitterService.Instance.Initialize( // change (1) - (3) for your app
    		"(1) ConsumerKey",
    		"(2) ConsumerSecret",
    		"(3) CallbackUri"); 
    	await TwitterService.Instance.LoginAsync();
    	tweets = await TwitterService.Instance.SearchAsync("hello world");
    	foreach (var tweet in tweets)
    	{
    		Debug.WriteLine(tweet.Text);
    	}
    }
    
  • And that’s all it is to search Twitter for e.g. “hello world”!

Things to note:

  • Even though the API is really easy, it doesn’t mean that you can avoid reading or at least scanning the Twitter documentation altogether – you need to know just enough the get the required information for login.
  • Storing the ConsumerKey and ConsumerSecret in an app as plain text doesn’t sound like a good idea – I’ll have to read up on what best practices are in regard to security.

No Comments