Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Delphi Code Monkey: 20 Years as a Delphi Developer May 26, 2015
      Delphi Code Monkey: 20 Years as a Delphi Developer Warren Postma's TDUG talk on YouTube. Highly recommended, there's a lot of insight both looking back and forward. I happen to have the same preference for past Delphi IDE versions: 7, 2007 and XE7. It was nice to see some praise for Subversion ...
      Delphi and stuff (Ondrej Kelle)
    • Melbourne Meeting – June 2015 May 26, 2015
      Monday 15th June at 6 for 6:15pm start. “hacking” binary data files – Andrew Tweddle Andrew  “hacks” binary data files to extract meaningful data he will discuss his techniques. The Clipboard and HTML – John McDonald John will do a short presentation based on his ...
      Australian Delphi User Group Members (Roger Connell)
    • Complete Martin Harvey’s Multithreading Article Online May 24, 2015
      Threading in Delphi, or in any other tool for that matter, is difficult.  There are a lot of good resources out there for Delphi developers, but perhaps none better than Martin Harvey’s “Multi-threading: The Delphi Way”.  There is a copy of it here —  ...
      Delphi-losophy (nickhodges@gmail.com)
    • Questions that Reveal the Importance Of Good Tools and Add-Ons for Delphi Users May 23, 2015
      I'm working on a review of the new Nexus Quality Suite, which I'll be posting as soon as I've finished it.  I just wanted to write a quick post about a very simple topic, that I think might be a good question for every delphi shop's developers, and every single-person delphi developer to think ...
      Delphi Code Monkey (Warren Postma)
    • Introducing the upcoming TMS Planner for FireMonkey May 23, 2015
      We're thrilled to introduce to you the TMS Planner for FireMonkey, in short TTMSFMXPlanner that has been in development for many months and that is near to being released now. While TMS Planner for FireMonkey design benefits from the significant experience the TMS team built-up with the award- ...
      TMS Software Blog
    • Woot: AsciiImage for Delphi http://memnarch.bplaced.net/blog/?p=129 May 21, 2015
      Woot! AsciiImage for Delphi by Alexander Benikowski on GitHub repository Memnarch/AsciiImage.. AsciiImage for Delphi –jeroen via: And here it is: AsciiImage for Delphi http://memnarch.bplaced.net/blog/?p=129.Filed under: Delphi, Development, Software Development
      The Wiert Corner (jpluimers)
    • Interview #3 - Jim McKeeth and Damien Bootsma May 21, 2015
      While attending the Auckland (New Zealand) Delphi XE8 launch event last month, I managed to get some video time with Jim McKeeth and Damien Bootsma. Watch the video on YouTube, you'll need to set aside about 40 minutes.
      LearnDelphi.TV (alister@learndelphi.tv)
    • Sydney Meeting – May 2015 May 21, 2015
      Thuirsday 28th May 2015 at 6 for 6:15pm start. This month’s Sydney meeting will be an informal “show and tell” style meeting. The idea is that anyone can have 10-15 minutes to show anything they like. It might be something from their code or something interesting they’ve ...
      Australian Delphi User Group Members (Mathias Burbach)
    • Scalable Systems [Part One] – Introduction to Scalable Systems May 20, 2015
      Foreword This article is written with the presumption of little-to-no prior knowledge of the subject on the part of the reader. Those experienced in the subject may find much of the information to be rather basic, and may even become frustrated with the approach I’ve taken in its authorship. I ...
      OTAPI (Simon J Stuart)
    • RAD Studio XE8: Learn What Developers are Saying & Offers Ending May 31st May 20, 2015
      Here's a copy of an email that will be going out this week with some info on what developers are saying about RAD Studio XE8 and a reminder of the special offers ending May 31. This is the US and Canada version. The buttons don't look quite right when pasting from the email into here but you can get ...
      Delphi Insider (Tim Del Chiaro)

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/.

About these ads

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: