Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Brisbane Meeting – March 2017 February 28, 2017
      Wednesday March 1st at 5:30pm to 9pm. Scott Hollows will be doing a remote presentation to the Brisbane ADUG chapter. TOPIC – Displaying and Animating 2D controls in 3D Spin and zoom buttons, images and other controls in 3D with just a few lines of code using Scott’s utility units All ...
      Australian Delphi User Group Members (Eivind Bakkestuen)
    • New book by +Nick Hodges tech preview by +Stefan Glienke – dependency injection in delphi February 27, 2017
      [WayBack] New book by +Nick Hodges tech preview by +Stefan Glienke https://leanpub.com/dependencyinjectionindelphi – via Jeroen Wiert Pluimers – Google+: This book covers Dependency Injection from A to Z.  In it you’ll learn about Constructor Injection, Property Injection, and ...
      The Wiert Corner (jpluimers)
    • Delphi Programming Index February 27, 2017
      Delphi celebrated its 22nd anniversary on February 14th and since it was Valentine's Day, people had some fun with #ILoveDelphi and some blog posts about their personal history with Delphi. Embarcadero even has a Delphi T-shirt design contest going on, which seems like a fairly reasonable geek thing ...
      Glooscap Software (Bruce McGee)
    • Yes, Starter Editions is Free February 27, 2017
      I still get people asking about Embarcadero offering a free or community version or Delphi or C++Builder. That is one of the big strategies of IDERA – offering good free tools. So when we joined IDERA it didn’t take long before we started offering Delphi and C++Builder Starter edition ...
      The PodCast at Delphi.org (Jim McKeeth)
    • The Malcolm Groves Rubbish Bin Connection Episode February 27, 2017
      Talking with Malcolm Groves of Code Partners in Sydney, Australia about his smart, connected rubbish bins.  
      The PodCast at Delphi.org (Jim McKeeth)
    • Delphi’s DNA Infographic February 24, 2017
      Remember my blog post about Delphi’s DNA? We’ve been working on making it into an Infographic, and now people are asking about creating a poster of it too! What do you think? Do you want a DNA poster for your wall? What suggestions or changes would you make?
      The PodCast at Delphi.org (Jim McKeeth)
    • Kommentar zu MVVM Anwendungen mit Delphi und nicht gegen Delphi von Olaf Monien February 24, 2017
      Freut mich, wenn es gefallen hat
      Olaf Monien (Olaf Monien)
    • Making the Glyph Property High DPI Aware for TBitBtn and TSpeedButton February 24, 2017
      Finally, last step in making my (/your) Delphi application not only high-dpi aware but also high dpi button-glyph-display-size-as-expected-ware. In my last post I’ve shared how to programmatically upsize images in TImageList so that menus, popups, toolbars and other controls using image lists ...
      Zarko Gajic (zarkogajic)
    • IoT and the Smart Grids February 24, 2017
      In this blog I will discuss IoT and then Smart Grid revolution that is changing the way electricity is going to be supplied over the next few years. Why is this important? – To build connected devices and services that help businesses and homes drive a greener IT agenda, you need to understand ...
      Stephen Ball (Stephen Ball)
    • Kommentar zu MVVM Anwendungen mit Delphi und nicht gegen Delphi von Michael February 24, 2017
      Zuerst einmal vielen Dank für den interessanten Vortrag. Wie würden in einem MVVM schreibende Prozesse realisiert werden, so dass Eingaben von mehreren Edits in die DB geschrieben werden? Der Verdrahtungsauswand im View soll ja minimal sein. Wie würde dann weiterhin Rückmeldungen in die View ...
      Olaf Monien (Michael)

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: