Developing a Simple Twitter App with Visual Basic and Windows 8.1

Introduction

It seems as if I have a fetish for social media apps this month… Probably. Twitter is basically a short message service system for the internet. I have always felt a bit limited with only 140 characters, but I guess Facebook makes up for it. With today’s article, I will demonstrate how to make a very basic Twitter application by using VB.

Getting Started

Before you can develop any application that makes use of Twitter’s functionalities inside the Twitter API, you have to register an application. This is where Twitter is a bit, let me say, they need a bit of work in this department. First, I am a relatively new Twitter user. I started tweeting, even before I got the confirmation email from them a whole two weeks later. Now, when you navigate to this link, they basically force Fabric on you. Getting any decent documentation on how to use their developer libraries is quite a headache. If it weren’t for Google, I probably never would have stumbled upon the documentation and libraries which you can find here and here.

I am a very finicky person, ask my wife… I am a perfectionist, ask my boss… Anyway, due to the fact that I have struggled so much to get to Twitter’s tools, I was already put off completely and almost dumped this project. Google the saviour helped again and I came across a few places that thankfully supply third-party Twitter wrappers. If you haven’t heard about the term wrapper before, read through this interesting article.

Enough talk, let’s play

Our Project

In today’s toy project, you will create a simple VB Windows Store app to search through Twitter, as well as to tweet a sample picture with the help of Linq2Twitter—nothing to complicated. If you haven’t read through Linq2twitter’s extensive documentation yet, I’d advise you to so now before proceeding.

Design

Create a new VB Windows Store app and design it according to Figure 1.

Twitter1
Figure 1: Our design

As usual, you may name the objects anything that please you, but it may differ from my object names.

Add a reference to Linq2twitter, as shown in Figure 2.

Twitter2
Figure 2: Linq2twitter reference

Before proceeding to the code, you have to add a config file and add the following in there:

<add key="consumerKey" value="YourKey"/>
   <add key="consumerSecret" value="YourSecret"/>
   <add key="accessToken" value="AccessToken"/>
   <add key="accessTokenSecret"
      value="AccessTokenSecret"/>

Have a read through here on how config files work and what they are. The information you have to supply in here will have been provided by Twitter after you have registered your application on Twitter, as explained before.

Code

Add the Linq2twitter namespace above the Class declaration:

Imports LinqToTwitter

Declare the modular variables:

   Private twitterctx As TwitterContext

   Private twAuth = New SingleUserAuthorizer() With { _
      .CredentialStore = New SingleUserInMemoryCredentialStore() With { _
         .ConsumerKey = ConfigurationManager.AppSettings("consumerKey"), _
         .ConsumerSecret = ConfigurationManager.AppSettings("consumerSecret"), _
         .AccessToken = ConfigurationManager.AppSettings("accessToken"), _
         .AccessTokenSecret = ConfigurationManager.AppSettings("accessTokenSecret") _
   } _
}

   Private tweet As Status

The first object creates a TwitterContext object. This object handles all the Twitter processing and requests, as you will see a bit later. The second object, twAuth, is your OAuth object. In case you do not know about OAuth, have a read through this interesting article explaining its uses. Basically, it aids in acquiring authentication information of the application, as well as the developer. The last object, tweet, is our physical tweeting object.

Add the next piece of code:

   Private Async Sub btnSearch_Click(sender As Object, _
      e As RoutedEventArgs) Handles btnSearch.Click

      Dim Response = Await (From search In twitterctx.Search _
         Where search.Type = SearchType.Search AndAlso _
         search.Query = "Microsoft").SingleOrDefaultAsync()

      If Response IsNot Nothing AndAlso Response.Statuses _
         IsNot Nothing Then

         Response.Statuses.ForEach(Function(tweet) _
            txtTweet.Text = "User: {0}, Tweet: {1}", _
            tweet.User.ScreenNameResponse, tweet.Text)

      End If

   End Sub

Okay, this looks complicated, but actually isn’t. First, I created a Response object and requested it to search Twitter for the string Microsoft. If you haven’t seen Async and Await before, have a read through “Async Programming with Visual Basic and Windows 8/8.1”, that explains it in detail. I then establish if there is a response or not. If there is a decent response, it must display all the tweets containing the phrase “Microsoft” inside the textbox.

Add the next piece of code:

   Private Async Sub btnTweet_Click(sender As Object, _
      e As RoutedEventArgs) Handles btnTweet.Click
        twitterctx = New TwitterContext(twAuth)

      Dim strTest As String = "Testing... "

      Dim blnSensitive As Boolean = False

      Dim decLat As Decimal = twitterctx.NoCoordinate
      Dim decLong As Decimal = twitterctx.NoCoordinate

      Dim Coords As Boolean = False

      Dim strImage As String = "..\..\images\200xColor_2.png"

      Dim btImage As Byte() = File.ReadAllBytes(strImage)

      tweet = Await twitterctx.TweetWithMediaAsync(strTest, _
         blnSensitive, decLat, decLong, Nothing, Coords, btImage)

      If tweet IsNot Nothing Then
         txtTweet.Text = "Sent" & tweet.Text
      End If
   End Sub

Here, I created a new TwitterContext object with the appropriate authentication. Then, I created a few objects whose values will be fed to the TweetWithMediaAsync method. This method fires and, if it was successful, it will display a simple message string inside the textbox. Okay, I know it’s a bit messy, but this is after all only a very basic introduction; I cannot do all the basic work for you as well.

Conclusion

That’s it! Short and sweet. Have fun building Twitter apps!

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read