Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Flotsam and Jetsam #98 March 29, 2015
      I’ve been whining about my difficulty in writing a book chapter about the Parallel Programming Library.  I’m getting the hang of it, though, and it is as cool as it’s been touted to be.  In fact, I’d venture to say that once you start using it, you’ll never really need to use `TThread` ...
      Delphi-losophy (nickhodges@gmail.com)
    • Delphi Pipe – Delphi related RSS feed running on Yahoo Pipes – via twm’s blog March 28, 2015
      New Delphi related RSS feed: Delphi Pipe “A collection of Delphi related RSS streams” which works perfectly fine in Feedly. So it is definitely time for me to really do some Yahoo Pipes research: Since Delphi Feeds doesn’t list my blog any more and generally isn’t very fast in responding ...
      The Wiert Corner (jpluimers)
    • TMS Software Spell Checking for VCL and FireMonkey apps March 27, 2015
      During several of our recent online events, I was asked if there was a multi-device spell checking and RTF component solution for FireMonkey. The answer is, yes there is.  Spell  Checking components are available from TMS Software both for Windows VCL and multi-device FireMonkey apps. ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • Latest Additions to TMS Workflow March 27, 2015
      TMS products could be categorized by "application user-oriented" or "application developer-oriented". Products like TMS Aurelius are heavily developer-oriented: it's the developer that "uses" the product during most of development time and the application user have no idea it even exists. Products ...
      TMS Software Blog
    • What does the Google Code shutdown mean for other services? March 26, 2015
      My brother lives in Provo, Utah, one of the few cities in the country fortunate enough to have Google Fiber service.  And they are truly fortunate; he describes it as awesome and amazing, the kind of Internet service everyone should have.  But the thing is… it’s from Google. Don’t ...
      TURBU Tech (Mason Wheeler)
    • An Interview with Ray Konopka March 26, 2015
      In this episode we talked to Ray Konopka during Delphi Week. Topics include the upcoming Delphi Developer Days, some of his work for Disney, Raize Software, including his new Radiant Shapes, Windows 10 and HoloLense (even though I wish they would rename it). Looking forward to seeing everyone at ...
      The PodCast at Delphi.org (Jim McKeeth)
    • FireDAC Skill Sprint - BDE to InterBase Migration using FireDAC March 25, 2015
      Here are the resource links for my Thursday, March 26, 2015 FireDAC Skill Sprint - "BDE to InterBase Migration using FireDAC". The slide deck is also available on slideshare. FireDAC page - http://docwiki.embarcadero.com/RADStudio/XE7/en/FireDAC FireDAC and ODBC ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • Introducing Radiant Shapes by Raize Software March 25, 2015
      Radiant Shapes is a brand new component library that will brighten any FireMonkey app. The library contains 35 reusable shape controls that can be used in all facets of FMX UI development from style composition and button adornment to data visualization and dashboard design. Each control has been ...
      Delphi Insider (Tim Del Chiaro)
    • WideOrbit is hiring for lots of positions! March 24, 2015
      Greetings,Here's a quick list of open positions and what we're looking for. If you're interested in any of these, feel free to shoot me an email at aohlsson@wideorbit.comDelphi Developer: Lynnwood, WA, San Francisco, CA or Hoover, ALDelphi, SQL Multi-tier systems experienceSr Software Engineer – ...
      The Delphi Hacker's Corner (Anders Ohlsson)
    • Interfaces are great, but they can be a pain to debug. Forgetting one weak link can lead to Access Violation hell… March 24, 2015
      Paul Thornton posted a nice question on G+ two weeks ago: Interfaces are great, but they can be a pain to debug. Forgetting one weak link can lead to Access Violation hell. I seem to remember a utility that would let you visualise the links between interfaced objects, but can’t remember what ...
      The Wiert Corner (jpluimers)

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: