Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Quick Tip: Implement Zoom In Virtual TreeView Using CTRL + / CTRL – April 29, 2016
      Most Windows applications I use in my daily work (email clients, browsers, text editors) have a handy feature allowing to make the text larger by increasing (or smaller by decreasing) the font size. Increasing the text size in a web browser is something I got accustomed to doing frequently – to ...
      Zarko Gajic (zarkogajic)
    • Faster FileStream with TBufferedFileStream April 29, 2016
      Buffering the File Stream in Delphi / C++ Builder Part of the very fast FireDAC database component library over recent releases was TFDFileStream,a class that allowed the streaming of large data (typically blobs) from the database server. Well, it seems it was too good to keep hidden and with the ...
      Stephen Ball (Stephen Ball)
    • Weak and Unsafe Interface References in Delphi 10.1 Berlin April 28, 2016
      Delphi 10.1 Berlin adds a couple of features to the Delphi language, bridging gaps between platforms and extending the Win32/Win64 interface reference model. This is not the only change to the language, but clearly the most relevant one. Another, focused on mobile, is the introduction of the native ...
      Marco's Tech Blog (marcocantu)
    • BigInteger support in DWScript April 28, 2016
      DWScript now has support for the BigInteger type, which supports numbers with as many decimals as the memory allows. This support comes in two flavors, either through a MPIR dll (a GMP fork) or through Rudy Velthui’s BigIntegers unit. The support can be added by having a reference to the ...
      DelphiTools (Eric Grange)
    • MVP of the Week: Yilmaz Yörü April 28, 2016
      Dr. Yilmaz Yörü was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He received his M.Sc. and Ph.D. degrees from the same department/university. Some of his interests are Programming, Thermodynamics (Exergy), Fluid ...
      The PodCast at Delphi.org (Jim McKeeth)
    • RAD Studio 10.1 Berlin Launch Webinar Replay April 27, 2016
      The replay of last week launch webinar of RAD Studio, Delphi and C++Builder 10.1 Berlin by RAD Product Managers (myself included) is available on YouTube. Some information about the webinar replay is also at http://community.embarcadero.com/blogs/entry/webinar-replay-introducing-rad-studio- ...
      Marco's Tech Blog (marcocantu)
    • Some RAD Studio 10.1 Berlin Blogs and Videos April 27, 2016
      Here is a first batch of blogs posts and videos about RAD Studio 10.1 Berlin. More are coming, including an in-depth webinar next week. 10.1 Berlin New Installer and IDE Great Installation Experience with new RAD Studio 10.1 Berlin at http://community.embarcadero.com/blogs/entry/great- ...
      Marco's Tech Blog (marcocantu)
    • Session-Based Test Management Software with TestRail April 27, 2016
      In this posting we are going to review how teams use session-based test management techniques with our test management tool TestRail. With more and more teams switching to agile development practices, adopting continuous integration and deployment, and with ever and fast changing requirements, the ...
      blog.gurock.com (Dennis Gurock)
    • Introducing TMS FNC UI Pack April 26, 2016
      TMS FNC UI Controls, a set of framework neutral controls Introduction As your customers use an ever increasing number of devices & operating systems, it is a daily challenge for us, software developers to make our software available for the myriad of target platforms in use. Fortunately, as ...
      TMS Software Blog
    • Sydney Meeting – April 2016 April 26, 2016
      Wednesday 27th April 2016 at 6 for 6:15pm start.   Important changes Courtesy of The Continuity Group we’re meeting in the boardroom of the Regus Serviced Office Centre in Ultimo. It’s a short walk from Central Station or there is on-street parking in nearby Mary-Anne St.… ...
      Australian Delphi User Group Members (lachlangemmell)

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: