Twitter library for Delphi

Edit: As of December 2012 Twitter api is using GZip in every callback, so you’ll have to enable content encoding before every Get/Post call, and then load the result from a gzipped stream in the RequestDone procs.

Edit June 2012: This twitter library is now deprecated and will no longer be updated. I’ve rewritten it as a generic OAuth lib to use with both Twitter and Imgur (and any other OAuth 1.0 services) for a closed source project.

This is my take on Open Auth and Twitter for Delphi. I recently had to look into it to update a twitter plugin for my Mal Upater application, so I made this small library along with a test application TwitMee. There is barely nothing at all for Delphi, and the little
I found was overcomplicated and didn’t work with Unicode (f.e. japanese symbols etc) (my library does).

I’ve been asked by a few people to make it open source, so here’s the code, use it freely as long you comply with the GPL v3 license.

Requirements: one of the latest versions of Delphi which includes Unicode. (probably easy to adapt using Jedi’s jcl implementation
for unicode strings as well).

// start by defining and creating the twitter object:
Var
Twit: TwitterCli;
Twit := TwitterCli.Create(TKey, TSecret);
with Twit do
begin
// You can specify a TMemo here for debugging purposes, which will output relevant
// data sent and received to twitter.
// DebugMode := True;
// DebugMemo := Memo1;
OnReqDone := TwitterCallBackProc;
RefURL := 'http://eden.fm';
end;
// TwitterCallBackProc is a simple method that's called after each request:
procedure TTwitMeMain.TwitterCallBackProc(Sender: TObject);
// TKey and TSecret are your app key and secret you get from Twitter after
// you register your app with them
// you can also specify a callback for TriggerBeforeSocketCall
// which will trigger just before a socket call. Here you can alter
// the socket to specify a proxy by accessing Twit.HTTPClient
// (as per ICS THttpCli properties)
// Next we need to request a token from Twitter:
// If you had already done this step before you can skip it, look at the
// end of this doc for details
Twit.RequestToken;
// Right after the call, control is given back to your application, since the
// call is asynchronous (non-blocking). the callback proc will be called after
// after we get the initial token from twitter, we need to send the user to
// twitter.com so they will login and get a PIN code to enter in your app
Twit.RequestPIN;
// you could also use Twit.RequestPinURL
// to get the URL in a string if you wish to copy it to the user in the
// clipboard or any other means, in case your app fails to launch the URL
// in the browser
// once done, the user has to give the PIN to us, in an edit box for example,
// then we tell the library the pin and request the final access tokens:
Twit.AccessPIN := Edit1.Text;
Twit.RequestAccess;
// our callback proc will be called once again and we are ready to send
// messages to twitter
// always check Twit.LastHttpStatus in the callback proc, if not 200 then
// something went wrong. You can check the variables ResultStrings.Text
// and LastHttpStatus for details on what went wrong
// now we can send a message with
Twit.SendTwit('test message');
// you can also use unicode here, like Japanese kanjis
// tested also with problematic symbols like # $ ! ' & etc
// * Note about access request: after you complete the acess steps,
// you can store these 2 string variables:
//
// Twit.AccessToken
// Twit.AccessTokenSecret
// and simply do: SetStoredLogin(a,b);
// after creating the twitter object (a and b being those 2 vars we stored
// and your app is ready to send without any previous steps
// And that's it, pretty simple. Note that this is a very simple implementation
// and we should check for other status codes from Twitter. I will be updating
// the library to fully implement the entire API eventually.
// a little more help. this is what my callback proc on TwitMee looks like:
procedure TTwitMeMain.TwitterCallBackProc(Sender: TObject);
begin
if Twit.LastHttpStatus <> 200 then
begin
Label1.Font.Color := clRed;
Label1.Caption := 'Error communicating with Twitter.';
Exit;
end;
if Twit.LastReq = trRequestToken then
begin
FlatEdit1.Text := 'Enter PIN here and click on Send button';
Twit.RequestPIN;
Exit;
end;
if Twit.LastReq = trRequestAccess then
begin
Label1.Font.Color := clWhite;
Label1.Caption := 'Ready to send.';
Authenticated := True;
FlatEdit1.Text := '';
SaveSettings;
Exit;
end;
if Twit.LastReq = trTwit then
begin
FlatEdit1.Text := '';
Label1.Caption := 'Message sent.';
Exit;
end;
end;

Hi Brian, thanks for the response, maybe I’m a bit stupid or something, but if I put that code on any line it simply gives me the error of
Undeclared Identifier Options, and Undeclared identifier httpoEnableContentcoding.
Clearly I am not really understanding what you are suggesting, though I have no doubt that what you say works when correctly implemented

Yeah.. that was a workaround I used at the time. Well I’ll do something, give me a couple days and I’ll release my new generic library with a new demo app for twitter; watch the front page for the post.

The above code doesn’t work for me because I get ” Undeclared Identifier ExtraSendHeader “. I have tried to look that up but all I can find is some relatively old posts on ICS where it seems to say that it is to do with a custom mod that you made to ICS , but would no longer be using because there was some slightly modified code that made it unnecessary.
I would sincerely appreciate a solution.
Many thanks

Is it possible to give some more detailed instructions for those of us that aren’t that clever. I am using D2007 and I can’t really get it to work at all and I am sure it is related to what you are saying above

[re:As of December 2012 Twitter api is using GZip in every callback]…so you’ll have to enable content encoding before every Get/Post call,I guess that means adding this line (please confirm)
Options := [httpoEnableContentCoding];
…and then load the result from a gzipped stream in the RequestDone procs.
Please give an example of the Delphi code I need to put in the RequestDone procs.
I’m using ICS V1.94 which I believe already handles gzip content encoding.

Hello Brian.
I’m testing your code but I have some trouble with accented characters which are really common in Italian language. If i try to tweet the message “Questa è una prova”, the TwitMee app gets an OAuth error. How can I solve this?

Hi brian, thanks for posting this code. I’ve been comparing this code to other delphi twitter libraries and am still a bit unclear on how to make it work. As below it would be great to get the source to the latest TwitMee app so I could see where I was going wrong. please send to adamr99 at gmail.com

This is a nice work you have done:)
Unfortunately, I am having difficulties in making the library work with a test sample i built with delphi 2010.
If it is possible, I would love as well to receive the source code for the TwitMee 0.22. My email is victorvicente at netcabo.pt
Thanks a lot!

I was able to get it working in Delphi6 (and with Unicode too I think). I’m using the TntComponents for Unicode.
Great work and thanks for sharing your skills.
If you are interested I’ll let you know when my app. is released.

Hello,
I’ve spent a fair while trying to get your code to work in Delphi6.
Unfortunately I’m still getting ‘Status: 401 Unauthorized’ errors’.
I’m nearly there but am now stuck.
Would it be possible for you to send me the source for TwitMee V0.22 so I can see where I’m going wrong.
My email is ‘twitter at play-time.demon.co.uk’
Many Thanks in advance