Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Spotlight on OmniThreadLibrary August 28, 2015
      If you are not using OmniThreadLibrary yet, or if you are only using its low-level parts (CreateTask and company), I’m kindly inviting you to a 20-minute presentation “Simple Parallel Programming with Patterns and OmniThreadLibrary”, which will be transmitted as a part of the Embarcadero Technology ...
      The Delphi Geek (gabr)
    • See Whats Coming in Delphi and C++ Builder August 28, 2015
      September 2nd is due to be a very important day in Delphi & C++ Builder history, if the recent news from Microsoft and what I have collated from the “whats coming” posts recently. Register for the Delphi and C++ Builder “First look” Spoiler alert! If you only want to ...
      Stephen Ball (Stephen Ball)
    • Webinar : What's coming up in RAD Studio, Delphi, and C++ Builder. August 27, 2015
      I just found out about an upcoming webinar from Embarcadero, on September 2nd:Ride the Windows 10 Release Wave See What's Coming in RAD Studio, Delphi and C++BuilderAny improvements in Windows 10 support are of course welcome, but as a guy who works with large projects, this sounds good ...
      Delphi Code Monkey (Warren Postma)
    • Two More Days for the RAD Studio 2015 Developers Survery August 27, 2015
      The yearly RAD Studio developers survey is closing in 2 days. If you can find the time to participate, we'd appreciate. And here are a couple of tidbits from it. But first of all (and before you continue reading) if you haven't already done so, take the survey at ...
      Marco's Tech Blog (marcocantu)
    • OmniThreadLibrary 3.04b on XE8 August 27, 2015
      Just a quick note – OmniThreadLibrary 3.04b is now available on XE8 via GetIt. --- Published under the Creative Commons Attribution 3.0 license
      The Delphi Geek (gabr)
    • Starting FPC compiler changed to 2.6.4 August 26, 2015
      The starting FPC compiler (used to compile the latest trunk FPC) has been 2.6.2 for some time now. This has changed recently; to compile the latest trunk FPC you need FPC 2.6.4 which has not yet been pushed to official repositories. At the moment the easiest work-around is to upgrade to FPC 2.6.4 ...
      Delphi and stuff (Ondrej Kelle)
    • CodeSite and Konopka Signature Controls Webinar On-Demand August 25, 2015
      Here's an email announcing that the recent webinar with Ray Konopka on CodeSite and Konopka Signature VCL Controls (formerly Raize Components) is now available on-demand.There are also new links in this for a Konopka Signature VCL Controls trial and to buy or upgrade Konopka Signature VCL Controls ...
      Delphi Insider (Tim Del Chiaro)
    • Melbourne Meeting – September 2015 August 25, 2015
      Monday 21st September at 6 for 6:15pm start. The Annual General Meeting of the Australian Delphi User Group Inc. Location:    Royal Society Hall,   8 La Trobe Street,   Melbourne. The meeting we have to have. Annual reports and election of office holders in accordance with our rules.… ...
      Australian Delphi User Group Members (Roger Connell)
    • RAD in Action: Develop a Complete Solution for Windows + Mobile + Web Service + Cloud August 24, 2015
      RAD in Action: Develop a Complete Windows + Mobile + Web Service + Cloud Solution Wednesday, August 26, 2015. 6am, 11am and 5pm Pacific Daylight Time In this RAD in Action webinar, you'll learn how to build a complete warehouse application for auction items that runs on Windows desktops and mobile ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • Delphi Blogs of the Week/Month #33 August 24, 2015
      Lots of news, despite the summer month, and also a recap of things I already blogged about, in case you were taking some week off. And the first sneak peaks to coming features... The Yearly Survey is Open As I already blogged about, take the yearly survey THIS WEEK ONLY: ...
      Marco's Tech Blog (marcocantu)

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: