Amazon.com Widgets Write a Twitter Client with Delphi XE7

Write a Twitter Client with Delphi XE7

By Nick at September 27, 2014 22:00
Filed Under: Delphi

I just wrote a Twitter client with Delphi XE7.  It took about thirty minutes, and most of that was spent trying to figure things out on the Twitter side of things.  Here’s the first tweet from my app:

It was surprisingly easy once you figure out the Twitter security. Here's how I did it.

First, I read the DocWiki on the REST client components.  It showed me where to go to create an application on the Twitter site.  Twitter uses OAuth1, and they have a page about it on their website.  Don’t worry, though.  Delphi includes a component that does all the work of OAuth 1.1 for you. 

The first thing you have to do is to create an app on the Twitter side.  I created an app called “DelphiDemo” and received a Consumer Key and a Consumer Secret.  I then went to the “Keys and Access Tokens” tab and generated an Access Token and and Access Token Secret.  Keep that page handy, because you are going to need the information on it in a minute.  And protect those keys – you shouldn’t distribute them.  I’ve blanked mine out in the shot below.

image

 

Next, you need the assistance of the large REST Demo that comes with Delphi. It does the heavy lifting of communicating with Twitter to get all the information that you need to create a client. It can be found here:

C:\Users\Public\Documents\Embarcadero\Studio\15.0\Samples\Object Pascal\Database\RESTDemo

Open it up, run it, and click on the “Twitter” tab.  You should see this:

image

 

The memo on the upper has the basic information on what to do.  You should have already created an app on the Twitter site.  Here is how it should go:

 

1.  Enter your Consumer Key, Consumer Secret, Access Token, and Access Token Secret from the Twitter web site into the appropriate edit boxes.

2.  Press the top button labeled “#1: Get Request-Token and Auth-Code”.  You’ll be asked to authorize your application for use on your account.  Do so. 

3.  Next, you should receive a second window with a number in it.  This number is a graphic and can’t be cut from the window.  Write this number down, as it is your “PIN” number to authenticate yourself.  Keep that number, you’ll need it to post to Twitter later.  You’ll also need it now, so put it in the “Auth-Verifier (“PIN’')” edit box. 

4.  Press “#2 Get Access-Token”.  That should fill out the Request-Token boxes and clear the Access-Token boxes.

5.  Now you should be ready to post a tweet.  If all has gone right, enter something in the Status edit box and press “#3 Send a tweet”. Check your timeline, and the tweet should be there.  Cool!

Okay, that is only half the battle. Now to create your own application.  Here’s the steps I took.

First, I created a new VCL application, and put an Edit Box, a Label, and Button on the form. 

Then, I went to the main form in the RESTDemo application and copied the OAuth1_Twitter component and pasted it on my new form. 

From there, I filled out all the properties on the component that I could.  The API endpoints should all be filled out – all you have to do is to enter your personal information from the app’s Twitter page into the component.

image

 

Be sure to include the VerifierPIN value that you write down earlier. Now, the rest is all Delphi. 

1.  Drop a TRestClient, a TRestRequest, and a TRestResponse on your form.

2.  Add the following method to your form:



procedure TForm42.ResetRESTComponentsToDefaults;

begin

  /// reset all of the rest-components for a complete

  /// new request

  ///

  /// --> we do not clear the private data from the

  /// individual authenticators.

  ///

  RESTRequest1.ResetToDefaults;

  RESTClient1.ResetToDefaults;

  RESTResponse1.ResetToDefaults;

end;

2.  Double click on the button on your form and add the following:



procedure TForm42.Button1Click(Sender: TObject);

begin

  ResetRESTComponentsToDefaults;

  RESTClient.BaseURL := 'https://api.twitter.com';

  RESTClient.Authenticator := OAuth1_Twitter;



  RESTRequest.Resource := '1.1/statuses/update.json';



  RESTRequest.Method := TRESTRequestMethod.rmPOST;

  RESTRequest.Params.AddItem('status', Edit1.Text, TRESTRequestParameterKind.pkGETorPOST);



  RESTRequest.Execute;

end;

3.  Put the following code in the OnChange event for Edit1:



procedure TForm42.Edit1Change(Sender: TObject);

begin

  Label1.Caption := Length(Edit1.Text).toString;

end;

4.  You’ll need to add REST.Types to your uses clause as well.

And there you go.  Run it, type in a tweet (Keep it 140 characters or less!) and press the button, and you should have posted your first tweet from your very own Delphi application. 

The RESTDemo application has a bunch of good demos in it that show you how to connect to Facebook, DropBox and others.  As usual, Delphi wraps up the functionality in a set of components, making it pretty easy for you to get the job done in a jiffy. 

blog comments powered by Disqus

My Book

A Pithy Quote for You

"We all want progress, but if you're on the wrong road, progress means doing an about-turn and walking back to the right road; in that case, the man who turns back soonest is the most progressive."    –  C. S. Lewis

Amazon Gift Cards

General Disclaimer

The views I express here are entirely my own and not necessarily those of any other rational person or organization.  However, I strongly recommend that you agree with pretty much everything I say because, well, I'm right.  Most of the time. Except when I'm not, in which case, you shouldn't agree with me.