Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Delphi hinting directives: deprecated, experimental, library and platform October 1, 2014
      I’ve been experimenting with the Delphi hinting directives lately to make it easier to migrate some libraries to newer versions of Delphi and newer platforms. Hinting directives (deprecated, experimental, library and platform) were – like the $MESSAGE directive – added to Delphi 6. ...
      The Wiert Corner (Jeroen Pluimers)
    • NTCore: interesting site about about system internals and software security September 30, 2014
      I recently bumped into the NTCore website by Daniel Pisti. At a client without my own VMs, I wanted to create a DebugBreak like function in Delphi, which I remembered from my Turbo Pascal days to be something like Inline($CC). So searching for both Delphi and INT 3, I found an EXE injection page at ...
      The Wiert Corner (Jeroen Pluimers)
    • Fixing XE7 + Android SDK 23 ZipAlign Problem September 29, 2014
      If you install RAD Studio XE7 and then upgrade Android SDK to version 23, you won’t be able to deploy applications to Android devices anymore. The reason for that is that ZipAlign.exe was moved out of sdk\tools folder in SDK 23 and XE7’s deployment process cannot find it anymore. Now it is located ...
      The Delphi Geek (gabr)
    • RAD Studio XE7: What Developers are Saying September 29, 2014
      This is a copy of a recent North America email for RAD Studio XE7. Sharing some of the great feedback so far about the new XE7 versions of RAD Studio, Delphi and C++Builder along with a reminder about the special offers ending September 30th.RAD Studio XE7: What Developers are SayingXE7 is more ...
      Delphi Insider (Tim Del Chiaro)
    • Write a Twitter Client with Delphi XE7 September 28, 2014
      I just wrote a Twitter client with Delphi XE7.  It took about thirty minutes, and most of that was spent trying to figure things out on the Twitter side of things.  Here’s the first tweet from my app: It was surprisingly easy once you figure out the Twitter security. Here's how I did ...
      Nick Hodges (nick@nickhodges.com)
    • PAServer HotFix for Delphi XE7 and RAD Studio XE7 to support iOS 7.1 simulator in XCode 6 September 27, 2014
      PAServer HotFix 09/26/2014 Hotfix for deploying iOS applications built with Delphi XE7 to the iOS 7.1 simulator With Delphi, C++Builder and RAD Studio XE7, we support deploying applications to iOS devices running both iOS 7 and iOS 8 out of the box. For deploying to the iOS Simulator with Delphi ...
      Sip from the Firehose (David Intersimone)
    • Video: What's New in Delphi XE7 September 27, 2014
      See what's new in Delphi XE7 for rapidly building highly connected apps for Windows, Mac, iOS, Android, gadgets, and wearables.Learn more about Delphi XE7  |  Download Delphi XE7  |  Special Offers
      Delphi Insider (Tim Del Chiaro)
    • VCL in XE7: Styles September 26, 2014
      Even if they have been in the product for a few versions, styles have seen several improvements (like the recent addition of menu styling in XE6). But what is a VCL Style? Here is a tentative definition: A style is a collection of painting rules you can dynamically apply to an entire ...
      Marco's Tech Blog (marcocantu)
    • Modernize Your VCL Applications with RAD Studio and Delphi XE7 September 26, 2014
      Below is some new info from the Embarcadero web site for VCL developers with info on why you will want to use RAD Studio XE7, Delphi XE7 and C++Builder XE7 to modernize your VCL apps.If you haven't tried the new version yet, click here for a free trial:Download RAD Studio XE7Download Delphi ...
      Delphi Insider (Tim Del Chiaro)
    • Leaflet Map With Google Maps and Geolocation September 26, 2014
      Here is a leaflet.js map, using plugins to show various sources including OpenStreetmap, MapBox and Google Maps. It also includes a search facility using the OSM geolocator. It’s mostly here for my own development use. You can pass a lat/lng on the query string to auto centre and zoom the ...
      David Glassborow

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: