Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Keep Threading in Mind April 22, 2017
      In my interview with Martin he talked a lot about the importance of performance, which is one of the reasons he uses Delphi for his real-time lighting control systems. He said something along the lines that if you are not programming with threading in mind then you are never going to get good ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Delphi Relax Web Scripting on Linux April 21, 2017
      I've finished porting my Delphi Relax web scripting library to RAD Studio 10.2, adding Linux support. I also added a new set of unit tests. The library is available at https://github.com/marcocantu/DelphiRazor/ and we are considering adding it to GetIt as well. In two weeks I'm going to host a ...
      Marco's Tech Blog (marcocantu)
    • The Niagara Falls Lighting Episode April 21, 2017
      This episode is extra special in that it include a case study of the software behind the lights for Niagara Falls. Nick and I spoke with Martin Searancke of Dream Solutions, Ltd. in New Zealand, who is the architect of the Light Factory software used by the Niagara Falls Illumination Board to ...
      The PodCast at Delphi.org (Jim McKeeth)
    • InterBase Developer Survey 2017 April 20, 2017
      Have your say on the future of InterBase Following the release of InterBase 2017, it is now time for the InterBase Developer Survey for 2017 – open now until the 14th May. Help shape the future of the award winning InterBase database by taking the InterBase Developer Survey 2017 and telling us ...
      Stephen Ball (Stephen Ball)
    • Gunny Meets Uncle Bob - Part 1 April 20, 2017
      The last few days have been very interesting. It started when my friend Jon Aasenden posted a link to a you tube video called "SOLID Principles of Object Oriented & Agile Design" by some guy named Uncle Bob. This wasn't the first time I heard about Uncle Bob. Nick Hodges made a reference to ...
      Cape Cod Gunny Does Delphi (Michael Riley)
    • When BRCC32 throws `Fatal error Illegal macro definition in command line or defines page.` in a DUnitX project April 19, 2017
      I had a Fatal error Illegal macro definition in command line or defines page. thrown by BRCC32.exe in one of my Delphi projects. As cgrc.exe could build the .rc file [source in Russian; WayBack], I reproduced it from the console with an Empty.rc file that has no content. That way I could rule out ...
      The Wiert Corner (jpluimers)
    • Linux ClientDataSet libmidas.so Now Available April 19, 2017
      To use the ClientDataSet component on Linux Delphi applications you need to distribute the libmidas.so library, the Linux equivalent of midas.dll. We failed to deliver it as part of the 10.2 Tokyo release, and it has been made separately available at: ...
      Marco's Tech Blog (marcocantu)
    • Introducing Hydra 5 April 18, 2017
      Today, I'm happy to announce the immediate availability of Hydra 5, the next major version of our beloved library for mixing and matching development platforms. For two versions now, Hydra has been the go-to solution for developers looking to mix Delphi and .NET code in the same application (among ...
      RemObjects Blogs (marc hoffman)
    • Jan Barendregt is Looking for programmers to maintain existing Excel add-in software April 18, 2017
      Jan Barendregt is Looking for programmers to maintain existing Excel add-in software. The reason is no fun (cancer without curative treatment and prognosis till death likely months). Please follow-up on the forum if you can be of help: [WayBack] Embarcadero Discussion Forums: Looking for programmers ...
      The Wiert Corner (jpluimers)
    • Reasonable User Interface (UI) Automation April 18, 2017
      The stories I hear about UI automation are often fantastic or tragic. Some of those fantastic projects have thousands of tests running on parallel virtual machines that start with every new code check in. These stories usually involve sophisticated tools to discover whether a test is flaky, or if ...
      blog.gurock.com (Kathryn Nest)

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/.

Advertisements

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: