Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Did you get the Memo ? July 31, 2014
      People looking for a cheap Android tablet have a new option from a respected player: The Asus Pad 7 Although not exactly falling over themselves in excitement (it is an entry level, budget device after all), reviewers are finding a lot to like in this device. But Delphi developers hoping to sell ...
      Te Waka o Delphi (Jolyon Direnko-Smith)
    • AES encryption envelope and Xamarin PCL July 31, 2014
      Recently I’ve been asked a question about how the AES encryption envelope can be used with a PCL build of Data Abstract, the issue being that PCL doesn’t expose the needed cryptography API. Although I don’t known the reason for this, I am still going to provide a solution. Before ...
      RemObjects Blogs (Anton)
    • New link for Ray Konopka webinar July 31, 2014
      I previously posted about Embarcadero's Developer Skill Sprints, and in particular, today's session with Ray Konopka. Unfortunately, if you registered for the skill sprints, the confirmation you received may be for sessions that begin on September 5th, skipping today's sessions. Here is the direct ...
      Glooscap Software (Bruce McGee)
    • Developer Skill Sprint: Effectively Using Delphi Generics with Ray Konopka July 31, 2014
      Join us for the Developer Skill Sprint: Effectively Using Delphi Generics with Ray Konopka today! Register for a session now: Thu, Jul 31, 2014 6:00 AM – 7:00 AM PDT Thu, Jul 31, 2014 11:00 AM – 12:00 PM PDT Thu, Jul 31, 2014 5:00 PM – 6:00 PM PDT
      The PodCast at Delphi.org (Jim McKeeth)
    • Learn about generics from Ray Konopka July 30, 2014
      Over the past couple of months Embarcadero has been hosting Developer Skill Sprints.  Each one is a short presentation concentrating on a specific topic or feature, followed by a Q&A session. There is a new skill sprint every Tuesday and Thursday which is shown at 6:00 AM and 5:00 PM PDT. ...
      Glooscap Software (Bruce McGee)
    • Final days for RAD Studio, Delphi and C++Builder XE6 July Offers July 30, 2014
      There are just a couple days left for the July 2014 special offers on RAD Studio, Delphi and C++Builder XE6.Recharge from XE5 to XE6 - get the low recharge price and get a free Bonus Pack of additional software including VCL and FireMonkey Style Packs, Mida Converter, and the popular Coding in ...
      Delphi Insider (Tim Del Chiaro)
    • Delphi: workaround for when you loose many of the ModelMaker Code Explorer keyboard shortcuts July 29, 2014
      ModelMaker Code Explorer (MMX for short) stores the keyboard shortcuts in the registry. I’ve had it occur once that somehow most (about 95%) of the shortcuts got lost. Two thinks I learned from resurrecting the shortcuts: MMX writes these settings when you exit Delphi Only a few keys (with ...
      The Wiert Corner (Jeroen Pluimers)
    • Movie #104 - Loops with Guard Clauses July 28, 2014
      Sometimes loops can get quite complex with lots of conditional logic and deep nesting.  Have a look at this video for a technique to reduce this complexity.
      LearnDelphi.TV (alister@learndelphi.tv)
    • Introducing VSoft.CommandLineParser for Delphi July 27, 2014
      Command line parsing Pretty much every delphi console application I have ever written or worked on had command line options, and every one of the projects tried different ways for defining and parsing the supplied options. Whilst working on DUnitX recently, I needed to add some command line options, ...
      VSoft Technologies
    • Come on Baby Light My Fire July 25, 2014
      These are exciting times in the mobile development space, especially for followers of RemObjects work. Whilst the likes of Xamarin and Embarcadero pursue their cross-platform abstractions, with varying degrees of success, RemObjects have been focussing on delivering genuinely native solutions and ...
      Te Waka o Delphi (Jolyon Direnko-Smith)

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: