Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Flotsam and Jetsam #107 August 3, 2015
      David Millington of Parnassus Software – author of the great and free Bookmarks plugin —  has released a new commercial IDE plugin called Navigator.  Looks pretty sweet. The book is this close to being done.  I just have to get it past the folks at CreateSpace.  Actually, ...
      Delphi-losophy (nickhodges@gmail.com)
    • IDE Fix Pack 5.93 – Windows 10 support August 3, 2015
      Those who tried to use IDE Fix Pack on Windows 10 got the uncritical error message “failed: Faster IDE startup [IDE.Startup.Fast]” that appeared on every IDE start. The problem is that a byte sequence changed that IDE Fix Pack tries to find. Windows 10 uses “INT3” instead of “NOP” instructions to ...
      Andy's Blog (Andreas Hausladen)
    • InterBase officially supports Windows 10 August 3, 2015
      In case you missed the big news last week, Windows 10 was released by Microsoft. My early feel for Windows 10 is that its going to be a game changer for the Windows platform. – Its the first platform that includes free upgrades for customers as far back as Windows 7 and brings the best of the ...
      Stephen Ball (Stephen Ball)
    • Setting Up a Parallel Build System August 2, 2015
      OmniThreadLibrary now supports 11 different Delphi versions (2007, 2009, 2010, XE, XE2, XE3, XE4, XE5, XE6, XE7, XE8), some with very special requirements about the supported pascal syntax (2007 and 2009 clearly standing out in that regard) so it takes quite some time to test the compilation of all ...
      The Delphi Geek (gabr)
    • Melbourne Meeting – August 2015 August 1, 2015
      Monday 17th August at 6 for 6:15pm start. How Unit Testing is Used by Melbourne ADUG Members. Members were invited to explain haw they used unit testing. Brian, Ian and Roger responded and will each give a brief presentation on how they make use of unit testing in their projects.… Continue ...
      Australian Delphi User Group Members (Roger Connell)
    • Beacons, Beacon Fencing, and the Internet of Things August 1, 2015
      It is a spectacular time for developers who want to interact with the real world of things: sensors, gadgets, devices, wearables, beacons and more. Developers are not just stuck writing code for desktop, client server and multi-tier systems. Developers can now use pre-built components and servers to ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • Vote for RAD Studio in the CTIA Super Mobility Awards August 1, 2015
      Help support RAD Studio, Delphi and C++Builder by voting for RAD Studio XE8 in the CTIA Super Mobility 2015 Awards.RAD Studio is nominated in the "Mobile Applications, Development & Platforms" category.Click here and vote for RAD StudioThanks for your help and for supporting RAD Studio and ...
      Delphi Insider (Tim Del Chiaro)
    • CodeRage X Call for papers and Pre-registration are open now! July 30, 2015
      CodeRage X is coming October 13-15, 2015Call for papers and pre-registration are open now!Embarcadero Technologies has begun planning for our tenth annual CodeRage online conference. The theme for this year’s CodeRage conference is “Develop Anything, Anytime, Anywhere.” The CodeRage X ...
      Delphi Insider (Tim Del Chiaro)
    • TCiphers – Multithreading Support. July 30, 2015
      TCiphers supports encryption/decryption in multiple threads for stream ciphers and block ciphers in CTR mode of operation. The idea is simple – to encrypt N bytes of data in m threads you split the data into m parts, each part approximately of N/m bytes size, then create m TCipher instances ...
      The Programming Works (Serg)
    • Delphi: turn off column or line block selection mode – via: Default IDE Shortcut Keys; Delphi Programming July 30, 2015
      The Default IDE Shortcut Keys – Delphi Programming was a good starting point to find out how to disable column or line blocks. Since the table there is incomplete (even the Embarcadero documentation is wrong as some shortcuts can turn on and off a mode), here are the relevant shortcuts keys: ...
      The Wiert Corner (jpluimers)

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

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: