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.
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:
Open it up, run it, and click on the “Twitter” tab. You should see this:
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.
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:
/// reset all of the rest-components for a complete
/// new request
/// --> we do not clear the private data from the
/// individual authenticators.
2. Double click on the button on your form and add the following:
procedure TForm42.Button1Click(Sender: TObject);
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);
3. Put the following code in the OnChange event for Edit1:
procedure TForm42.Edit1Change(Sender: TObject);
Label1.Caption := Length(Edit1.Text).toString;
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.