Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • DelphiMVCFramework at the 10° Delphi Meetup in Dresden , Germany February 9, 2016
      Mathias Pannier did a nice talk at the 10° Delphi Meetup in Dresden, Germany. You can find his review with the slide (both in english) in his blog. Mathias contacted me about some specific questions the audience did. Here’s my replies. Is it possible to have a ...
      Daniele Teti's programming blog (Daniele Teti)
    • The Year of the FireMonkey February 8, 2016
      Today is the Lunar New Year starting the Chinese year of the Fire Monkey. The first day of the New Year falls on the new moon between 21 January and 20 February, which in 2016 is Monday, February 8th. The animal associated with the year is cyclical, as is the elemental sign. The last year of the ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Buy One Embarcadero Developer Tool and Get Another Tool Free - Until Feb 29, 2016 February 8, 2016
      Buy One Embarcadero Developer Tool and Get Another Tool Free. Get even more value from RAD Studio, C++Builder and Delphi 10 Seattle. The offer ends February 29, 2016. For a limited time, with each qualifying Embarcadero RAD Studio, Delphi or C++Builder product you buy, you can choose a second ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • Delphi 2016 Roadmap February 8, 2016
      The RAD Studio roadmap for 2016, including plans for Delphi and C++Builder for the current year, is now public. You can find it at: http://community.embarcadero.com/article/news/16211-embarcadero-rad-studio-2016-product-approach-and-roadmap-2 As you can see, there ...
      Marco's Tech Blog (marcocantu)
    • InterBase Journaling – InterBase Labs February 8, 2016
      InterBase Journaling We (geeks) should all know that applications that run in memory run faster than those that have regular disk I/O. Having more of the data processing in memory has a positive effect on speed and performance,  however its not without risk. As memory is transient, you are always at ...
      Stephen Ball (Stephen Ball)
    • Delphi Cookbook 2nd edition has been announced February 8, 2016
      Just after 1 year and half from the 1st edition, the 2nd edition of Delphi Cookbook has been announced. The first edition got a lot of interest and appreciation comments, so the editor decided to ask me a 2nd edition. So here it is! It is still in work-in-progress but the work is going smooth. In a ...
      Daniele Teti's programming blog (Daniele Teti)
    • 10 Reasons to Use PAServer for Remote Windows Deployment February 5, 2016
      Everyone knows PAServer (Platform Assist Server) is the easy way to deploy and debug multi device apps from Windows to Apple OS X and iOS. It handles all the bundling and deployment from your Windows development platform across the network, via a virtual machine, or even to the cloud. Previously I ...
      The PodCast at Delphi.org (Jim McKeeth)
    • When your license check is faulty and causes customers to loose work you a cardinal sin February 4, 2016
      I wrote about this before, named it a cardinal sin too, but I seem to have to repeat this: When your product thinks the license is validate and quits without allowing the customer to save its work, then you’ve committed a cardinal sin. Yes, I can talk about cardinal sins: I’ve been named ...
      The Wiert Corner (jpluimers)
    • Melbourne Meeting – February 2016 February 4, 2016
      Monday 15th February at 6 for 6:15pm start. Melbourne Wireless – Peter Buncle Melbourne Wireless aims to promote the development of wireless networking within the City of Melbourne, in particular the building of a free, community owned broadband network. Peter will talk about some of the ...
      Australian Delphi User Group Members (Roger Connell)
    • Poll: What type of Delphi shop are you? February 4, 2016
      What type of Delphi shop are you?Enjoy - Semper Fi Gunny Mike
      Cape Cod Gunny Does Delphi (Michael Riley)

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: