Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Meet Me in Chicago & D.C./Baltimore for a First Look at XE6 April 18, 2014
      I’m joining Bob Swart and Cary Jensen for the U.S. cities of their annual Delphi Developer Days 2014 tour with the keynote and a free evening event. During my keynote and the evening session I’ll show off Google Glass development with XE6, as well as using an EEG Brain-Computer Interface ...
      The PodCast at Delphi.org (Jim McKeeth)
    • XE6 - New Look and Feel April 18, 2014
      One of the first things long time Delphi Developers may notice about XE6 is that it has new look and feel.One thing I have learned over the years is that when you change the user interface in any way you will get a divergent number of opinions.    My opinion of this change is simple: ...
      Rob's Technology Corner (Robert Love)
    • VCL Developers' Guide to Delphi XE6, C++Builder XE6 and RAD Studio XE6 April 18, 2014
      There's a great new webinar coming up on April 30th featuring Marco Cantù. This is your chance to get a more in-depth look at some of the new things VCL application developers can do with RAD Studio, C++Builder and Delphi XE6.RAD in Action: VCL Developer's Guide to RAD Studio XE6Featuring Marco ...
      Delphi Insider (Tim Del Chiaro)
    • DDevExtensions, DFMCheck and ByteStrings for XE6 April 18, 2014
      DDevExtensions 2.81 adds support for RAD Studio XE6. DFMCheck 1.5 supports RAD Studio XE6. System.ByteString for RAD Studio XE6 gives you the (hidden) native Byte-String types for all mobile platforms back.
      Andy's Blog (Andreas Hausladen)
    • Are Your Software Sales Where They Should Be? April 17, 2014
      As an Independent Software Vendor running a one-person, part-time business I'm always wondering if sales are where they should be. If I'm honest with myself I say "No". Trying to understand why can be a very difficult.I rediscovered this article called "If No Independent Developers Are 100 Times ...
      Cape Cod Gunny Does Delphi (Michael Riley)
    • ICS updated for Delphi XE6 April 17, 2014
      ICS has been updated for Delphi XE6. No new feature, just a simple update for the latest Delphi version. VCL and FMX supported for desktop platforms. Mobile still under way. Download as usual. See http://wiki.overbyte.be Follow me on Twitter Follow me on LinkedIn Follow me on Google+ Visit my ...
      Behind the connection (François Piette)
    • Emerging Languages Camp Part 3: Noether April 17, 2014
      This is the third post of my notes from Emerging Languages Camp last year. If you haven’t seen it already, you might want to read the Introduction to this series. Noether: Symmetry in Programming Language Design Daira Hopwood Slides · Presentation I found this presentation to be at once fascinating ...
      Craig Stuntz's Weblog (Craig Stuntz)
    • Delphi Dev. Shell Tools now supports Delphi XE6 and Appmethod April 17, 2014
      The Delphi Dev. Shell Tools now supports Delphi (RAD Studio) XE6 and Appmethod , also the new version include some fixes and new features. You can download the shell extension from the project page.
      The Road to Delphi (Rodrigo)
    • XE6 Menu Styling (and a Thank You) April 17, 2014
      One of the new features of the VCL in Delphi (and C++Builder) XE6 is the support for styling in menus. This is an image showing the feature in action for the main menu of a styled application. (Notice, there is no code you need to write, only recompile a VCL styled application in XE6 or enable ...
      Marco's Tech Blog (marcocantu)
    • Video: What's New in RAD Studio, Delphi and C++Builder XE6 April 17, 2014
      Check out this video that covers some of the key new features of Delphi XE6, C++Builder XE6 and RAD Studio XE6.
      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: