Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Early Looks at the Next Version of RAD Studio, Delphi and C++Builder March 6, 2015
      The next version of RAD Studio, Delphi and C++Builder hasn't been announced yet but you'll have several opportunities to get early information and an early look over the coming weeks.Also keep in mind that if you purchase RAD Studio, Delphi or C++Builder XE7 between February 23 and March 31, 2015, ...
      Delphi Insider (Tim Del Chiaro)
    • InterBase Change Views Demo and Video March 6, 2015
      InterBase Change Views Example Demo This week I hosted a Skill Sprints session looking at InterBase Change Views with an example set of code working around a Pharmacy needing to keep centralised drug information updated at distributed pharmacies. The example code will ship with the next major ...
      Stephen Ball (Stephen Ball)
    • Delphi Live Tutorial in the Netherlands and CodeMonkey March 6, 2015
      We had 160+ enthusiastic Delphi developers on the "Develop a complete Windows, mobile, web service and cloud solution in 1 day!" event yesterday in Hoofddoorp, The Netherlands! Read more at my embarcadero community blog! Share This | Email this page to a friend
      Pawel Glowacki (Pawel Glowacki)
    • Provable Optimization with Microsoft Z3 March 6, 2015
      A few months ago, some coworkers sent around a Ruby challenge. It appears simple, but we can sometimes learn a lot from simple problems. Write a Ruby program that determines the smallest three digit number such that when said number is divided by the sum of its digits the answer is 20. In case ...
      Craig Stuntz's Weblog (Craig Stuntz)
    • Software Conservatives and Liberals March 5, 2015
      I ran across a great article again, one I originally read six month ago, written by Steve Yegge (Googlier, used to work at Amazon, outspoken about many design topics), and remembered how great it was: His original post The followup I won’t ruin the article for you, but I think this is a ...
      David Glassborow
    • Brutal way to prevent memory errors in Delphi XE7: rename DLLs, loose part of the functionality, but less memory issues – via G+ March 5, 2015
      When the suggestions from Out-of-Memory in RAD XE7 and -noparser from Tried to patch BDS.exe fails to, then this suggestion by Brian Hamilton on G+ might work: From a post in the EMB forum, a work around for XE7 is I follow the hint of Thomas Grusche renaming the following files: ...
      The Wiert Corner (jpluimers)
    • Breathe New Life into Your Apps with App Tethering March 4, 2015
      Here's a copy of an email from Embarcadero that you may have seen today if you're in North America and may see soon if you're anywhere else in the world.Breathe New Life into Your Apps with App Tethering“XE7's new app tethering capabilities, like Bluetooth support and more control over subnet ...
      Delphi Insider (Tim Del Chiaro)
    • Delphi anonymous method formatting thread going into the direction of functional programming – via: Markus Joos – Google+ March 3, 2015
      Markus Joos started a nice G+ thread on anonymous method formatting which ended up into a discussion of functional programming and a peek into what Spring4D 1.3 can offer. Note you might need to be part of the G+ Delphi Developers Community to read the thread. –jeroenFiled under: Delphi, ...
      The Wiert Corner (jpluimers)
    • RAD Studio 2015 Roadmap – Blogs March 3, 2015
      Odd that the RAD Studio 2015 Roadmap on the Embarcadero site got published by Brian Alexakis which is relatively unknown in the Delphi community. I wish that ARC for Windows and Linux x86/x64 were on the list: the first since it makes it so much easier to solve ARC problems in libraries, the second ...
      The Wiert Corner (jpluimers)
    • 500th Blog Post and New RAD Studio Roadmap March 2, 2015
      Hard to believe but this is my 500th blog post. Thank you to everyone who follows this blog and the Delphi Insider Twitter, and for the 1.2 million views so far. I hope you find the information useful.And for this special 500th blog post, some exciting news with an updated RAD Studio ...
      Delphi Insider (Tim Del Chiaro)

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: