Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • RAD Studio Berlin 10.1 Notes and Links May 25, 2016
      In case you are already using RAD Studio Berlin 10.1 or are merely looking into it, these links and notes may help you. What’s New in RAD Studio 10.1 Berlin Using C++ lambdas with Parallel Programming Library Marco Cantu on [weak] and [unsafe] attributes My [weak] vs. [unsafe] demo FireUI preview ...
      The Delphi Geek (gabr)
    • June 16 Webinar - RAD Server Application Deep Dive - Creating a complete Department Store Retail Solution May 25, 2016
      During this webinar you'll learn how to develop and deploy a complete real world Department Store Retail application using Embarcadero RAD Server. The client app runs on smartphones and the server side executes on IIS production servers on premises and on Amazon. The content covered on this ...
      Sip from the Firehose (davidi@embarcadero.com)
    • Fire Tips: Working with Build Messages May 25, 2016
      If you have worked with Fire a bit, you will have noticed that unlike most other IDEs, it has no "Error List". This is not an oversight, but by design. Fire integrates error (and similar) messages in the jump bar, as well as directly, inline, with the code editor. Let's look at some common and best ...
      RemObjects Blogs (marc hoffman)
    • Faster Compilation for Delphi Win32 Generics in 10.1 Berlin May 25, 2016
      While the Delphi compiler is recognized as an extremely fast compiler, when it comes to generics it can significantly slow down, along with the linker. The latest version of Delphi offers a significant improvement. During the development of 10.1 Berlin Delphi’s R&D team, and in particular ...
      Marco's Tech Blog (marcocantu)
    • Brisbane Meeting – June 2016 May 25, 2016
      Wednesday June 1st at 5:30pm to 9pm. Calling all Delphi-ites! The next Brisbane meeting, on Wednesday June 1st, is one you don’t want to miss. Please note, the meeting is open for anyone who wishes to attend; an ADUG membership is not required.… Continue reading ›
      Australian Delphi User Group Members (Eivind Bakkestuen)
    • June 2 Webinar: Getting Started Building Mobile Applications for iOS and Android May 24, 2016
      This webinar will show you how to build mobile applications for iOS and Android using Delphi and C++Builder 10.1 Berlin. We will cover getting started, best practices for mobile UI/UX, building your first app, using FireUI Live Preview, creating custom design views and Live Previews, a real world ...
      Sip from the Firehose (davidi@embarcadero.com)
    • Delphi XE8 does not adhere the `$(PROJECTNAME)` in a “Unit Output Directory”, but does recognise `$(SanitizedProjectName)`… May 24, 2016
      The background here was a quick project at a client where many .dproj files were in the same directory, but they suffered from conditional define differences. Which meant that even if they were all using the  DEBUG configuration, some defines in the .dproj directories were different therefore ...
      The Wiert Corner (jpluimers)
    • Melbourne Meeting – June 2016 May 24, 2016
      Monday 20th June at 6 for 6:15pm start. Anonymous Methods and Generics – Colin Johnsun Colin has presented many times at ADUG meetings in Sydney. We have invited him to Melbourne and he has chosen to roll his Sydney presentation on “Anonymous Methods” in with the one on ...
      Australian Delphi User Group Members (Roger Connell)
    • More New MVPs May 24, 2016
      I’ve got another large group of MVPs that have just signed up. They will appear in the MVP directory soon, but until then you can discover more about them here: Joachim Dürr – Germany Malcolm Groves – Australia Oren Aviram – Isreal Samer ASSIL – Turkey Andrew Efimov ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Presenting the TMS software website redesign May 23, 2016
      Since 1995, the year tmssoftware.com started focussing on offering components & tools for developers, it was time for a fifth major overhaul of our website. Since the beginning of this year, a lot work went into this overhaul behind the scenes for this overhaul. Some of the main goals of this ...
      TMS Software Blog

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: