Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Centennial Bridge: BUILD, Creators Update, Windows 10 S and More May 29, 2017
      As you probably know, RAD Studio 10.1 Berlin Update 2 last fall was the first IDE to offer native and direct support for the Windows Desktop Bridge (also known as Centennial Bridge). In short, using the bridge you can create Universal Windows Platform (UWP) applications that can be distributed using ...
      Marco's Tech Blog (marcocantu)
    • FireDAC Book – Using InterBase May 26, 2017
      New book – Delphi in Depth: FireDAC There has been a growing stream of Delphi books in the last year covering everything from cross-platform development with FireMonkey to coding best practices around OOP. The latest book to be released is by long time Delphi Author Cary Jensen, specifically ...
      Stephen Ball (Stephen Ball)
    • My Visit to Australia and ADUG Symposium May 26, 2017
      Last week I was in Australia for the ADUG Symposium, invited by the ADUG committee. For more information about this user groups, which is one of the largest and best organized in the Delphi community, refer to https://www.adug.org.au/. The event was nice and well organized, I ended up talking ...
      Marco's Tech Blog (marcocantu)
    • TMS XData REST Server on Linux - Step by Step - Part 5 May 25, 2017
      This is part 5 of the tutorial on how to create TMS XData and TMS Sparkle servers on Linux. On Part 4, we have added Sparkle to our Apache module, preparing it to receive any modules we want to add. And in this final part we will add the TMS XData module to it. Here is video for part 5: Adding the ...
      TMS Software Blog
    • Announcing Elements 9.1 May 25, 2017
      This week, we have shipped the long anticipated Elements 9.1, the latest version of or multi-platform, multi-language compiler tool chain. I'm very excited about this release, and an extensive overview of what's new can be found on our all-new website, here. But i want to highlight a few areas that ...
      RemObjects Blogs (marc hoffman)
    • UsesHelperSetup.zip by Stefan Glienke for Delphi 10.0/1/2 May 25, 2017
      [WayBack] Some while ago I started a plugin to easily add units to the uses clause just like you can do via quickfix in VisualStudio.I never actually polished it… – Stefan Glienke – Google+ UsesHelperSetup.zip download: https://1drv.ms/u/s!ArjIoB43KhwDg3LZW8i23jTj5L2v (for now ...
      The Wiert Corner (jpluimers)
    • Delphi 10.2 Tokyo Godzilla ISO and other download links including hashes May 24, 2017
      I thought I already posted this, but since hashes were mentioned at [WayBack] Still can’t get Rad Studio 10.2 to install on my laptop. It’ll install now, but then it can’t load several BPLs and then it crashes before the IDE gets … – Phillip Woon – Google+ From ...
      The Wiert Corner (jpluimers)
    • RAD Studio 10.2 links May 24, 2017
      This post contains just a bunch of links to online resources that I mentioned on the today’s presentation. Read more »--- Published under the Creative Commons Attribution 3.0 license
      The Delphi Geek (Primož Gabrijelčič)
    • TMS XData REST Server on Linux - Step by Step - Part 4 May 24, 2017
      This is part 4 of the tutorial on how to create TMS XData and TMS Sparkle servers on Linux. On Part 3, we have created and deployed our first Apache Module using WebBroker. Now we're going to add TMS Sparkle to it. Here is video for part 4: Adding TMS Sparkle and using Sparkle modules in the Apache ...
      TMS Software Blog
    • The Checking and Testing Debate Explained: Everything You Need to Know… May 24, 2017
      This is a guest posting by Simon Knight. Simon Knight works with teams of all shapes and sizes as a test lead, manager & facilitator, helping to deliver great software by building quality into every stage of the development process. The terms “testing” and “checking” ...
      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: