Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • TMS coming to Delphi events near you April 25, 2015
      I'm honored to have been invited to two upcoming first-class Delphi events in Europe (United Kingdom and Germany) in May. I'm looking forward to meet again with so many fellow Delphi developers and discuss topics that excite us all software developers in a changing world. The focus in my session ...
      TMS Software Blog
    • Value Never Used – Or Is It? April 25, 2015
      Sometimes, dcc32 is just plain stupid … I have this code in the OmniThreadLibrary:function TOmniThreadPool.Cancel(taskID: int64): boolean;var res: TOmniWaitableValue;begin {$IF CompilerVersion >= 22} Result := false; // not really used {$IFEND} res := TOmniWaitableValue.Create; try ...
      The Delphi Geek (gabr)
    • A Response to Nik Hodges - You will pay for bugs and like it! April 24, 2015
      I tried to post a comment on Nik’s blog, but for some reason I never got the email for verification of my address so I thought I would respond here. I find it very interesting that a former EMBT employee still with close ties, would write such a post.  I understand how he drew some of his ...
      TPersistent.com (Larry Hengen)
    • Audit Log using TMS Aurelius events April 24, 2015
      The latest release of TMS Aurelius introduces an event system that allows you to subscribe listeners to some events that might be fired while you are using Aurelius, especially the TObjectManager. One key difference between regular Delphi events and Aurelius events is that the latter are ...
      TMS Software Blog
    • RAD Studio XE8 Launch Webinar - Encore Presentation April 23, 2015
      Live Webinar EncoreWindows and Beyond: See What's New inRAD Studio, Delphi and C++Builder XE8You asked, we listened.Due to the overwhelming demand, we're offering a live replay of the "Windows and Beyond: See What's New in RAD Studio, Delphi and C++Builder" webinar on Wednesday, April 29, 2015. ...
      Delphi Insider (Tim Del Chiaro)
    • You are going to pay for bug fixes. And you’re going to like it. April 23, 2015
      The day is coming when you are going to have to pay to get bug fixes.  You won’t like it at first, but that is the way things are heading.  And you should like it. I can remember the day when software came with free support.  You had a problem, you called up the company and spoke to a ...
      Delphi-losophy (nickhodges@gmail.com)
    • IDE Fix Pack 5.91 – Bugfix release April 23, 2015
      There was a bug in the TStringList.IndexOf performance optimization that didn’t take into account that DB.TFlatList overrides GetCount and implements lacy loading. The optimization directly accessed TStringList.FCount which was zero because GetCount wasn’t called. This bug appeared when you tried to ...
      Andy's Blog (Andreas Hausladen)
    • Delphi XE8 things I learned from the second week of G+ reading April 22, 2015
      General XE8 news: Delphi Library Guidelines | The Art of Delphi Programming. DST issue when converting DateTime: Can someone point me to the problem? I have this code: var dt, dt2:…. Delphi is not a cross-compiler, but a set of cross-compilers – Synopse. Delphi IDE Colorizer Now supports RAD ...
      The Wiert Corner (jpluimers)
    • DDevExtensions 2.83 for XE8 released April 21, 2015
      The DDevExtensions 2.83 IDE plugin is now available for RAD Studio XE8. The only change compared to 2.82 is that XE8 is supported. DDevExtensions download: Name IDE Version File Size Downloads Added DDevExtensions 1.61 5-2007 DDevExtensions161Setup.zip 734.07 KB 15452 times 2009-01-10 ...
      Andy's Blog (Andreas Hausladen)
    • Delphi on Windows x64: solution when project compiles fine under IDE, but not using MSBuild April 21, 2015
      Especially when using Delphi 2007 on Windows x64 system (8.x, 7, Vista, 2012 Server, 2008 Server, …), the Delphi specific .Targets files used by msbuild are simply installed to the wrong location. A better warning for this is in my BeSharp.net repository: Run-Dependend-rsvars-From-Path.bat ...
      The Wiert Corner (jpluimers)

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: