Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Free (and inexpensive) Tools From Embarcadero August 25, 2016
      I can (and sometimes do) argue the benefits of Delphi all day long, but if you're just starting out or aren't using it for paid development, the price can be a barrier to entry. So I was glad to see Embarcadero offering the Starter edition for free (until Sept 9th) and a Delphi Boot Camp, a free ...
      Glooscap Software (Bruce McGee)
    • September ... a busy 'event' month at tmssoftware August 25, 2016
      We're pleased that in the month September we have again the chance to meet our customers, to show our products, to meet new developers who wishes to learn about our components & tools, ... on several upcoming events in Europe. Bruno Fierens has also been invited as speaker, Bruno will talk about ...
      TMS Software Blog
    • Why sometimes you *want* to to have your DFM files stored as binary August 25, 2016
      Delphi Component/Tool vendors have to support a truckload of Delphi and C++ Builder versions which can be a pain: they have to work around problems in Delphi and C++ Builder versions that have long been abandoned by Borland/CodeGear/Embarcadero/Idera/… This means that sometimes the Delphi ...
      The Wiert Corner (jpluimers)
    • Get a Copy of Marco’s Object Pascal Handbook with Delphi Starter August 24, 2016
      As part of the Delphi Boot Camp and Delphi Starter Promotion, you can also get a copy of Marco Cantu’s updated Object Pascal Handbook, all at no cost. If you want a physical copy of Marco’s book, you can pick one up on his website, or just check out the table of contents . . . Part I ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Update to List-Delphi-Installed-Packages.ps1 shows HKCU/HKLM keys and doesn’t truncated fields any more August 24, 2016
      I’ve posted an update to List-Delphi-Installed-Packages.ps1 that: added HKCU/HKLM registry key values for each BDS version doesn’t truncate fields any more when your console has limited width is now in UTF-8 format so the BitBucket web-interface can show it For the first two: Phry for ...
      The Wiert Corner (jpluimers)
    • Blaise Pascal Issue 53 with Atanas Popov Interview August 24, 2016
      Tried something new where I went over the current issue of Blaise Pascal with Detlef Overbeek. The video is available on YouTube. INTERVIEW WITH ATANAS POPOV Atanas agreed after being asked immediately to be interviewed. That was about two months ago. About two weeks ago there was quite a lot of ...
      The PodCast at Delphi.org (Jim McKeeth)
    • LiveBindings for VCL Developers – Webinar! August 23, 2016
      LiveBindings for VCL Developers [Webinar] Visual LiveBindings is a technology that allows you to bind your data and objects (model) to the UI making. While LiveBindings is a key part of FireMonkey development, it is often overlooked by VCL developers. A while ago I started a series of blogs on ...
      Stephen Ball (admin)
    • Delphi Boot Camp August 23, 2016
      Delphi Boot Camp, 100% discount on Starter Edition and Marco’s Book! Delphi Boot Camp – 5th to 9th September 2016 Following on from the recent success of the C++ Builder Boot Camp, attended by programmers from 128 countries, its Delphi Boot Camp time!! Delphi Boot Camp Join Embarcadero ...
      Stephen Ball (Stephen Ball)
    • Free Delphi Starter Edition August 23, 2016
      Interesting offer by Embarcadero: Delphi Starter Edition Special Offer
      Delphi and stuff (Ondrej Kelle)
    • An Elements Update, aka 8.3.95 August 22, 2016
      Hi everyone. Last week, we shipped Elements 8.3.95, the second and final interim update for 8.3. This version has been a great release from the get-go, and .95 adds an incredible amount of polish, minor improvements and bug fixes. It also includes a whole bunch of changes and new features for Fire, ...
      RemObjects Blogs (marc hoffman)

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

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: