Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Delphi Blogs of the Week #54 July 21, 2017
      A new round up of Delphi-related news, after my two weeks of vacation around Europe. Delphi Product Blogs and More Quickly find add-ons with new GetIt categories by Sarina at https://community.embarcadero.com/blogs/entry/quickly-find-add-ons-with-new-getit-categories (by the way, we are open ...
      Marco's Tech Blog (marcocantu)
    • Testing and Beyond: An Interview with Ben Kelly July 21, 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. With my interest piqued by his new book, Standout, I decided ...
      blog.gurock.com (Simon Knight)
    • EurekaLog 7.6 is out July 21, 2017
      We are pleased to announce the availability of the new 7.6 version. EurekaLog is a tool for Delphi and C++Builder that gives your application the power to catch every exception and memory/resource leak, generating a detailed log of the call stack (with unit, procedure and line number), ...
      EurekaLog Blog (Александр Алексеев)
    • A First Peak at Water, our New IDE for Windows July 20, 2017
      Hi everyone. I wanted to take a moment and give a shoutout to let you all know that we've started shipping the first preview builds of Water, our upcoming IDE for Windows. While we're not quite ready for a public preview yet, Water has been available as a beta download for all users with an ...
      RemObjects Blogs (marc hoffman)
    • Postgresql Streaming Replication recipe July 19, 2017
      This is a quick recipe for PostgreSql 9.x Streaming Replication configuration between a Master and Slave servers based on https://www.howtoforge.com/tutorial/postgresql-replication-on-ubuntu-15-04/Master: IP 192.168.1.249Everything should be done as postgres user.psql CREATE USER replica REPLICATION ...
      Leonardo's blog (Leonardo M. Ramé)
    • Teaching Software Testing With Games July 19, 2017
      This is a guest posting by Justin Rohrman. Justin has been a professional software tester in various capacities since 2005. In his current role, Justin is a consulting software tester and writer working with Excelon Development. Outside of work, he is currently serving on the Association For ...
      blog.gurock.com (Kathryn Nest)
    • Getting Technical with Cary Jensen and FireDAC (Episode 74) July 19, 2017
      In this episode of the Podcast @ Delphi.org we talk with Cary Jensen about his new book Delphi in Depth: FireDAC and the upcoming Delphi Developer Days which Cary puts on with Dr. Bob Swart. Nick Hodges, our co-host, was a previous host of Delphi Developer Days with Cary, and I’ve made a ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Anyone knows if Raize subscription before the Raize acquisition is still valid? July 18, 2017
      It’s from a while ago, so I wonder if this is still true:   I just got my Embarcadero update to RC6 that includes #10Seattle support. –jeroen Source: Can I say that I’m a bit disappointed by the way the Raize acquisition has been…Filed under: Delphi, Development, Software ...
      The Wiert Corner (jpluimers)
    • Perth Meeting – July 2017 July 16, 2017
      Tuesday 18th July 2017 at 6pm, first presentation 6:15pm WHERE Artifactory  8/16 Guthrie St, Osborne Park WA 6017 FOOD  $1.50 pizza slice.  $1.50 drinks   Justin Dressler and Alister Christie from Learn Delphi TV presenting   Presentation 1) Justin Dressler – The Beast PC Justin ...
      Australian Delphi User Group Members (Scott Hollows)
    • Creating a Bootable USB Drive on macOS from an ISO July 16, 2017
      Creating a bootable USB drive from an ISO is pretty easy on Windows, but you end up going to the terminal on macOS. Not a big deal if you remember all the commands. Since it seems like I’m frequently installing something (Linux, Windows 10, etc.) on a different I thought I would make a quick ...
      The PodCast at Delphi.org (Jim McKeeth)

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: