Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Top 11 Computer Programmers of All Time July 22, 2016
      The problem with top 10 lists is they never include everyone, but this one goes to 11. My son helped me put this one together. Would love to hear your thoughts in the comments. Throughout the ages, many great minds have arisen from the masses and changed the course of humanity for better or for ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Australian 2016 Symposium – Spotlight on speaker Alister Christie July 22, 2016
      Alister Christie from LearnDelphi.tv is presenting at the Australian 2016 Symposium Find out more about Alister in this video interview. Alister’s presentation will give an overview of the current state of computational power including Parallel / Threaded programming and Distributed ...
      Australian Delphi User Group Members (Scott Hollows)
    • ADUG 2016 Interview July 22, 2016
      It's the ADUG Winter Symposium in a few weeks and I'm a presenter again - it's going to be fun so make sure you get your tickets.  While you wait with bated breath, watch this interview on YouTube with myself and Scott Hollows.
      LearnDelphi.TV (learndelphi@christie.net.nz)
    • Top 10 Reasons to be a Delphi Developer July 21, 2016
      I found this article on the top 10 reasons to be a Delphi developer. I’m curious what your top reasons to be a Delphi developer would include. Top 10 Reasons to be a Delphi Developer The job of a software developer is pretty interesting. You learn programming languages to develop a variety of ...
      The PodCast at Delphi.org (Jim McKeeth)
    • delphi – Building Multi threaded TCP/IP Server – Stack Overflow July 21, 2016
      On the research list: TIdSchedulerOfThreadPool (now that TIdThreadMgrPool is gone). Need to manage the lifetime of these as I don’t want the threads to live forever, but also don’t want to make the instantiation cost too high. Most likely I want to wait for them to finish as well upon ...
      The Wiert Corner (jpluimers)
    • InterBase – Webinar – The Ultimate Embedded Database July 21, 2016
      InterBase – The Ultimate Embedded Database InterBase is the ultra-fast, scalable award winning multi-platform embeddable SQL database with commercial grade data security, disaster recovery and change synchronization. Join me on the 28th July to explore why OEM, ISV and VAR’s the world ...
      Stephen Ball (Stephen Ball)
    • RAD Server Intro Video July 20, 2016
      Embarcadero has published on YouTube a nice video introducing RAD Server. If the embedded version below doesn't work, go to: https://youtu.be/HY0JRJPvjsU
      Marco's Tech Blog (marcocantu)
    • Australian Symposium Update – Spotlight on Keynote speaker Malcolm Groves (Code Partners) July 19, 2016
        In this spotlight interview we dig into the background of 2016 ADUG Symposium keynote speaker Malcolm Groves. ADUG 2016 SYMPOSIUM Thursday 4th August 2016 Sydney  /  Friday 5th August 2016 Melbourne You dont need to be a member to attend.  … Continue reading ›
      Australian Delphi User Group Members (Scott Hollows)
    • Delphi packages I have disabled by prefixing their description with an underscore (and why) July 19, 2016
      There is a little trick to disable “Known IDE Packages”: you can stop Delphi from loading one by either making “Value data” of the registry blank, or prepending it with an underscore: packages that might have been disabled, by checking any string entries where the data has ...
      The Wiert Corner (jpluimers)
    • Embracing .NET with RemObjects Hydra July 17, 2016
      If you're a Delphi shop and find yourself (sometimes) jealous of all the .NET goodies have a look at RemObjects Hydra (more details here). It's a framework which allows you to mix and use VCL, FireMonkey and .NET code modules in a single mixed-mode application. (They've recently started the "Escape ...
      Delphi and stuff (Ondrej Kelle)

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: