Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Developer Skill Sprints for Q3 2015 - The programming fun starts Tuesday, July 14, 2015 July 6, 2015
      Q3 2015 - Developer Skill Sprints: Code, Test, Version Control, Deployment and More This latest season of Developer Skill Sprints focuses on programming tips and techniques most requested by attendees of the past 72 skill sprints we’ve already presented. This quarter we’ll take your developer skills ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • Delphi VCL and FireMonkey: eBooks, Books, Tutorials and Courseware - a collection of links July 6, 2015
      We are often asked where developers and teachers can find books, courseware and tutorials available today that cover Delphi programming and Multi-Device FireMonkey app development. The great news is that there are more and more books appearing on Amazon, Lulu and online. Here is my latest list of ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • How to vibrate iOS and Android phones using FireMonkey and XE8 July 6, 2015
      Pawel Glowacki asked about how to vibrate a smartphone from Delphi code for iOS and Android. The following sample code for Delphi and FireMonkey was provided by Thierry Laborde and Williamm Wittwer from Embarcadero France: unit Umain; interface uses System.SysUtils, System.Types, System.UITypes, ...
      Sip from the Firehose (david.intersimone@embarcadero.com)
    • RAD Studio in Tel Aviv rocks! July 6, 2015
      Just back from Software Development Conference in Israel. There was probably around 600 attendees gathered in Hilton Tel Aviv hotel. It is always great to meet existing, loyal Delphi developers. They are very happy with the latest versions of RAD Studio. At conferences like this it is also ...
      Pawel Glowacki (Pawel Glowacki)
    • CCR.VirtualKeying – virtual keystroke interface for Windows and OS X July 5, 2015
      I’ve just added to GitHub a little virtual keystroke interface for Windows and OS X – on the former it wraps the SendInput API, on the latter the CGEvent functions: https://github.com/chrisrolliston/CCR.VirtualKeying Virtually typing some text into the active window is a one-liner: ...
      Delphi Haven (Chris Rolliston)
    • FPC and Lazarus development environment (follow-up) July 4, 2015
      I've just done a fresh installation of Linux Mint 17.2 'Rafaela' MATE 64-bit edition from scratch. I've followed my previous notes to set up my FreePascal and Lazarus development environment, with success.
      Delphi and stuff (Ondrej Kelle)
    • PDFium Delphi VCL example control July 4, 2015
      I’ve uploaded the PDFium Delphi VCL control code that was sitting on my disk for 2 months to GitHub. It contains the DLL bindings, an abstraction layer and an example VCL control.
      Andy's Blog (Andreas Hausladen)
    • Sending virtual keystrokes on OS X July 4, 2015
      Going through my comments backlog I found a question asking how to send virtual keystrokes on OS X. Short answer is to use the CGEvent functions in MacApi.CoreGraphics – in a nutshell, Call CGEventSourceCreate to allocate a CGEventSourceRef. For each keystroke, create a CGEventRef by calling ...
      Delphi Haven (Chris Rolliston)
    • Codetyphon 5.3 and Remobjects 8.3 : installation instructions July 4, 2015
      While i love to work on delphi side its lack support for some IOTs devices like raspberry PI, Beaglebone, and Linux mainly.You have two options sit and wait for Embarcadero managers take the decision and release the long waited linux compiler (10 years of wait i think) or get dirty and try ...
      Parallel Pascal Worlds (donaldshimoda)
    • Using OmniThreadLibrary’s Message Queue with a TThread Worker July 4, 2015
      A reader recently asked about using OmniThreadLibrary’s communication channels with a TThread-based worker thread so I prepared a simple example, now part of the OTL repository (stored in the folder examples\TThread communication). Two separate topics are covered in this example: Sending data from ...
      The Delphi Geek (gabr)

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: