Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • TODO: Check if Continua CI parsesl DUnit XML FinalBuilder emitted XML when using pure DUnit tests and TRepeatedTest ones September 29, 2016
      On my TODO list. References: Is the new 1.5.1.0 build of Continua CI supposed to be able to read the DUnit XML FinalBuilder emitted XML? Reproduced with just DUnit code. by jpluimers · Pull Request #4 · VSoftTechnologies/DUnit-XML 28239 XMLTestRunner for NUnit compatible output from DUnit This in ...
      The Wiert Corner (jpluimers)
    • Updating RAD Studio GetIt Package Manager Content for Berlin September 29, 2016
      We have recently gone back to updating and cleaning the content of GetIt packages, updating them to the last version and creating package versions for Berlin as needed. Plus we updated third party tools. While the work isn't finished, I felt it is worth underlying the significant progress we ...
      Marco's Tech Blog (marcocantu)
    • Delphi Mobile Point of Sale Software September 28, 2016
      Delphi Mobild Point of Sale Software : Android tablet or smartphone point of sale application using a WIFI connection to a DataSnap REST Server connected with FireDac to a FireBird database
      Felix Colibri
    • How to deploy a Delphi OSX project from the command line – kouraklis.com September 28, 2016
      This is so cool: How to deploy a Delphi OSX project from the command line – kouraklis.com [WayBack] I always wanted to hack the communication path to PAServer [WayBack] – despite issues – but never had the time. Luckily others had… See: Automated deployment of a Delphi OSX project ...
      The Wiert Corner (jpluimers)
    • I’m looking for an algorithm that converts a list of integers (for instance of… September 28, 2016
      Got a very quick response Range combo – Paste.ie – Irish For Pastebin on: I’m looking for an algorithm that converts a list of integers (for instance of pages to be printed) from this form:1,2,3,4,6,7,8,13,14into1-4,6-8,13-14… – Jeroen Wiert Pluimers – Google+ ...
      The Wiert Corner (jpluimers)
    • 10 Web Security Testing Tools Every Tester And Developer Should Know September 28, 2016
      “We don’t do security testing. That’s the job of the Pen Test team!” You’ve probably heard that before, right? Not recently though I hope, because if we’re all responsible for quality now, then we’re all responsible for security too – and the vast ...
      blog.gurock.com (Dennis Gurock)
    • The Oracle at Delphi: Set in my ways September 27, 2016
      A few pieces of Delphi compiler history: Allen Bauer: Most notably, the Object Pascal/Delphi compiler is written in mostly C with a smattering of C++, the editor kernel (sans display rendering) and debugger engine (process control/symbol table management) were written in C++. All of which I’ve ...
      The Wiert Corner (jpluimers)
    • Analyzing website performance with the Windows Performance Toolkit | Microsoft Edge Dev Blog September 27, 2016
      On my research list: Slow pages lose users: research from Bing and Google indicates that delays as small as half a second can impact business metrics. To build fast sites, developers need powerful tools to analyze the … Source: Analyzing website performance with the Windows Performance Toolkit | ...
      The Wiert Corner (jpluimers)
    • Delphi Blogs of the Week/Month #46 September 27, 2016
      It has been long time since I wrote a summary blog post, so this is just a collection of highlights. I'll try to get back to a decent frequency. Official Embarcadero News As you probably know we released RAD Studio Berlin Update 1. Check this blog post if you missed it. Embarcadero is ...
      Marco's Tech Blog (marcocantu)
    • Sydney Meeting – September 2016 September 27, 2016
      The next Sydney ADUG meeting will be on Wednesday 28th September 2016 at 6 for 6:15pm start Important changes Courtesy of The Continuity Group we’re meeting in the boardroom of the Regus Serviced Office Centre in Ultimo. It’s a short walk from Central Station or there is on-street ...
      Australian Delphi User Group Members (lachlangemmell)

Using OAUTH for Delphi

Posted by chuckbeasley on April 29, 2009

Before attempting to use OAUTH, you must obtain a developer key and secret from the service provider.  In the sample code provided in this article, I have removed my developer key and secret.  No other code modifications have been made.

Step 1 – Request request token (step A in diagram)

procedure TForm1.RqsBtnClick(Sender: TObject);
var
URL: string;
endpos: integer;
begin
Key := ‘http://www.myspace.com/developerkey’;
Secret := ‘developersecret’;
URL := ‘http://api.myspace.com/request_token’;
// Create all objects
Consumer := TOAuthConsumer.Create(Key, Secret);
HMAC := TOAuthSignatureMethod_HMAC_SHA1.Create;

ARequest := TOAuthRequest.Create(URL);

ARequest := ARequest.FromConsumerAndToken(Consumer, nil, URL);
ARequest.Sign_Request(HMAC, Consumer, nil);
HTTPStream := TStringStream.Create(”);
URL := URL + ‘?’ + ARequest.GetString;
Response := idHTTP1.Get(URL);

endpos := AnsiPos(‘&oauth_token_secret=’, Response);
oauth_token := ”;
oauth_token := Copy(Response, 13, endpos-13);
Response := Copy(Response, endpos, Length(Response));

oauth_token_secret := Copy(Response, 21, Length(Response));
Token := TOAuthToken.Create(oauth_token, oauth_token_secret);
end;

The service provider responds by sending a token and token secret, which is parsed from the Response variable.

Step 2 – Authorize (Step C in diagram)

procedure TForm1.AuthBtnClick(Sender: TObject);
var
Callback_URL, URL :string;
begin
URL := ‘http://api.myspace.com/authorize’;
Callback_URL := ‘http://www.chuckbeasley.com’;
URL := URL + ‘?’ + ‘oauth_token=’ + oauth_token + ‘&’ + ‘oauth_token_secret=’ + oauth_token_secret +
‘&oauth_callback=’ + TOAuthUtil.urlEncodeRFC3986(Callback_URL);
EmbeddedWB1.Navigate(URL);
end;

The token, token secret, and call back URL are appended to the URL.  The service provider obtains user authorization and directs the consumer to the call back URL.

Step 3 – Request access token (Step E in diagram)

procedure TForm1.AccBtnClick(Sender: TObject);
var
endpos: integer;
URL: string;
begin
URL := ‘http://api.myspace.com/access_token’;
Consumer := nil;
Consumer := TOAuthConsumer.Create(Key, Secret, ‘http://www.chuckbeasley.com’);
ARequest.HTTPURL := URL;
ARequest := ARequest.FromConsumerAndToken(Consumer, Token, URL);
ARequest.Sign_Request(HMAC, Consumer, Token);
URL := URL + ‘?’ + ARequest.GetString;
Response := idHTTP1.Get(URL);
endpos := AnsiPos(‘&oauth_token_secret=’, Response);
oauth_token := ”;
oauth_token := Copy(Response, 13, endpos-13);
Response := Copy(Response, endpos, Length(Response));

oauth_token_secret := Copy(Response, 21, Length(Response));
Token := TOAuthToken.Create(oauth_token, oauth_token_secret);
end;

The service provider responds by sending an access token and token secret, which is parsed from the Response variable.

Step 4 – Access protected resources (Step G on diagram)

procedure TForm1.AccRscClick(Sender: TObject);

procedure process(o: ISuperObject);
var
f: TSuperObjectIter;
i: Integer;
begin
case ObjectGetType(o) of
stObject:
begin
Memo1.Lines.Add(‘{‘);
if ObjectFindFirst(o, f) then
repeat
keypair := f.key;
process(f.val)
until not ObjectFindNext(f);
ObjectFindClose(f);
Memo1.Lines.Add(‘}’);
end;
stArray:
begin
Memo1.Lines.Add(‘[‘);
for i := 0 to o.AsArray.Length – 1 do
process(o.AsArray.O[i]);
Memo1.Lines.Add(‘]’);
end;
stString:
begin
keypair := keypair + ‘:’ +(UTF8Decode(o.AsString));
Memo1.Lines.Add(keypair);
end;
stNull:
Memo1.Lines.add(‘nil’);
else
Memo1.Lines.add(o.AsString);
end;
end;
var
json: ISuperObject;
StringList: TStringList;
URL: string;
begin
URL := ‘http://api.myspace.com/v2/people/40250975/@friends?count=10000&format=json’;
Consumer := nil;
Consumer := TOAuthConsumer.Create(Key, Secret, ‘http://www.chuckbeasley.com’);
ARequest.HTTPURL := URL;
ARequest := ARequest.FromConsumerAndToken(Consumer, Token, URL);
ARequest.Sign_Request(HMAC, Consumer, Token);
URL := URL + ‘&’ + ARequest.GetString;
Response := idHTTP1.Get(URL);

StringList := TStringList.Create;
StringList.Add(Response);

json := TSuperObject.Create(stObject);
json.Merge(Response);
json := json.O[‘entry’];
Memo1.Lines.Clear;
process(json);
end;

This step assumes that the profile owner has authorized the application to access its protected data.  That can be performed by adding the application to the profile.

OAUTH for Delphi is available via SVN:   http://oauthdelphi.svn.sourceforge.net/.

2 Responses to “Using OAUTH for Delphi”

  1. Ali said

    Hey,
    Do you have a working example of using oAuth with the new PIN-system?

  2. […] […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: