Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Fun with Delphi Contest: NASA API Mashup January 20, 2017
      Embarcadero is promoting a contest for applications that leverage OpenData, in particular the NASA API (available at https://api.nasa.gov/index.html). Given the large amount of data available, there is room for building many different applications. Also, you can submit Windows or Mobile ...
      Marco's Tech Blog (marcocantu)
    • Delphi “F2039 Could not create output file” on drc files January 19, 2017
      I had this error when building my Delphi app and needing detailed MAP files. Somehow, then Delphi also tries to generate .drc files and fails. Performing the “F2039 Could not create output file” drc – Google Search didn’t reveal much. So I fired up Process Monitor and looked ...
      The Wiert Corner (jpluimers)
    • Dproj changed or not changed? Normalize it! January 18, 2017
      I guess it is since Delphi 10 Seattle that dproj files are changed on disk even if no change was done to the project settings inside the IDE. This is a real PITA with version control systems. Actually there are no changes to the content – it is just reordered somewhat randomly. The only ...
      The Art of Delphi Programming (Uwe Raabe)
    • TMS Component Pack v8.5 released with wave of new components January 18, 2017
      We have given up thinking that the TMS Component Pack will ever be complete. It is simple: It will never be complete! On a daily basis, we're in conversation with developers around the planet with interesting, useful and cool new ideas. Between waking up and going to sleep, we ponder all the time ...
      TMS Software Blog
    • IoT Boot Camp – Rapidly building a connected network of devices. January 17, 2017
      Arduino and RAD Server / IoT Boot Camp IoT Boot Camp – From Arduino to any programming language On the 6th February Embarcadero is running a free developer IoT boot camp, specifically looking at making some cool IoT devices and then building a network of connected devices using ThingPoints™ ...
      Stephen Ball (Stephen Ball)
    • An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough. January 17, 2017
      I will probably need this again somewhere in the future: An exponential back-off implementation I used somewhere; probably room for improvement, but it works good enough. It’s Delphi, but I’ve not seen practical implementations in C# either. –jeroen Filed under: .NET, C#, Delphi, ...
      The Wiert Corner (jpluimers)
    • Delphi Blogs of the Week/Month #49 January 16, 2017
      The first list of interesting links and blog post of 2017, focused on Delphi development. Embarcadero Udpates Press release "Embarcadero Announces RAD Studio Desktop Bridge Support for Windows 10 Deployments" including a comment by Kevin Gallo, corporate vice president for the Windows ...
      Marco's Tech Blog (marcocantu)
    • RSS feed for Dave’s Development Blog – mostly OTA articles on the Open Tools API for Delphi and C++ Builder January 16, 2017
      TL;DR: http://www.davidghoyle.co.uk/WordPress/?feed=rss2 Since there is no RSS link  on the page [WayBack] Dave’s Development Blog – Software Development using Borland / Codegear / Embarcadero RAD Studio Since I wanted to follow his “blog” (which is sort of a collection of ...
      The Wiert Corner (jpluimers)
    • Perth Meeting – January 2017 January 15, 2017
      The Perth Delphi user group are meeting this week Tuesday  17th January 2017 at 6 for 6:15pm start Artifactory 8/16 Guthrie St, Osborne Park WA 6017 Map Scott Hollows Delphi 3D plus an Architecture for reusable forms, toolbars, grids Peter Thönell Load and save settings (an example of using the ...
      Australian Delphi User Group Members (Scott Hollows)
    • I need an Expert January 13, 2017
      Over the years various files that should be included in the Delphi installation package have gone missing such as MidasLib for 64 bit builds for instance and now ExpertsCreators.dcp (I still can’t get my head around the package name - shouldn’t it be “ExpertCreator.dcp”). I ...
      TPersistent.com (Larry Hengen)

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

 
%d bloggers like this: