Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Delphi programmers: Learn how your users actually use your software April 24, 2014
      And now, for something completely different: Have you ever wondered what happens with your software after you send it to users? What would you do if you knew which features of your software are used the most? Do you know how many people used your software yesterday? Today, I’m introducing ...
      TwoDesk Delphi Blog (jacob)
    • New Betas! April 24, 2014
      Hi everyone. Over the past weekend, we shipped major new beta builds for, well, just about every single one of our products.   On the Elements front (Oxygene and RemObjects C#) we’ve also shipped the first beta for the May 2014 update, which will be a minor bug-fix update, but has a lot of ...
      RemObjects Blogs (marc)
    • Splash screens in Delphi Android apps April 23, 2014
      I’ve been meaning to do it for quite a while and I recently started writing up my steps for creating an Android splash screen (as per my CodeRage 8 session). No sooner did I do so that Delphi XE6 launched itself onto the scene, making my write up immediately slightly out of date :-/ Unperturbed, I ...
      Brian Long (Brian Long)
    • TZDB 1.8.2014b April 23, 2014
      A new version of TZDB has been release (1.8.2014b). This version fixes a few issues around the Windows TZ aliases provided by CLDR project. Obviously the database has been updated to the most recent version (2014b).   The biggest news is the fact that we have moved to GitHub! The old Google ...
      Yet Another Programming Blog (alex)
    • Remote Object Persistence with Parse and Kinvy April 23, 2014
      New BaaS components introduced in RAD Studio XE6 now make it easily to use remote data storage services offered by Kivey and Parse along with Push notifications, File Storage and user authentication. This can greatly reduce the infrastructure that you need to code, develop and manage with your ...
      Stephen Ball (Stephen Ball)
    • Great blog post about App Tethering by Steffen Nyeland April 22, 2014
      App Tethering is a new capability in XE6. RTL components are included for connecting your VCL and FMX apps together on the same machines or multi-devices in your Object Pascal and C++ projects. XE6 includes the TTetheringManager and TTetheringAppProfile components for your use. Using app tethering ...
      Sip from the Firehose (David Intersimone)
    • Presenting XE6 Down Under (Australia, New Zealand, and Taiwan) April 22, 2014
      I'm going to be traveling "Down Under", that is to Australia, New Zealand (located at Italian antipodes), and Taiwan to present RAD Studio XE6 over the next two weeks, part of the "Embarcadero Developer Direct LIVE!" series. It looks like this is going to be an intense trip, with mostly 2 night ...
      Marco's Tech Blog (marcocantu)
    • Delphi Blogs of the Week #11 April 22, 2014
      A very busy week, with the release of Delphi XE6 and C++Builder XE6. Lots of links, grouped by area. XE6 Release My own blog post at blog.marcocantu.com/blog/2014_april_xe6avaialble.html has a rather large collection of links already. No need to repeat those. Introducing BAAS ...
      Marco's Tech Blog (marcocantu)
    • Call for help: Spring4D and Delphi MultiCast events: working for Win32, Win64 and Mac OS X x64, but Android/iOS ARM help needed. April 22, 2014
      Years after Allen Bauer and a few others wrote about Delphi MultiCast events, the Spring4D framework now supports MultiCast events on 3 platforms: Win32 Win64 Mac OS x86 Depending on the platform, the minimum required Delphi version is this (the develop branch builds in Delphi XE6): Win32: Delphi ...
      The Wiert Corner (Jeroen Pluimers)
    • More Raize Software updates for Delphi XE6: CodeSite Studio 5.1.5, Inspex 2.6.3, DropMaster 2.4.3. April 22, 2014
      After finding out that Raize Components 6 and Raize Components 5 updates for Delphi XE6 I checked Raize Software‘s latest news page and was glad to see that these also got updates: CodeSite Studio version 5.1.5. Inspex version 2.6.3. DropMaster version 2.4.3. The only one not yet having XE6 ...
      The Wiert Corner (Jeroen Pluimers)

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: