Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Developer Skill Sprints video links August 29, 2014
      After Bill Meyer asked, Helge Larsen wrote: The landing page for Developer Skill Sprints is here: https://www.embarcadero.com/landing-pages/skill-sprints The videos is on Developer Skill sprints YouTube channel first: https://www.youtube.com/playlist?list=PLwUPJvR9mZHhZTajVWsgaFPLtDA-t1Xwc ...
      The Wiert Corner (Jeroen Pluimers)
    • TToolBar’s TToolButton AutoSize Width Issues (With Empty Caption) August 29, 2014
      If you are using the TToolBar control with TToolButtons in your Delphi application (are you not?, in at least one :\) with “Enable runtime themes” enabled for your project, you might have noticed you cannot easily alter the width of an individual button. What’s worst, if you are using button images ...
      Zarko Gajic (zarkogajic)
    • TMS Day in Denmark on November 4th August 29, 2014
      We are excited to announce that Jens Fudge, Embarcadero MVP for Denmark, and TMS software are planning a TMS day in Denmark on November 4th. All day, sessions will be spent on TMS VCL components, TMS cloud components and TMS FireMonkey components with also sufficient session time allocated to ...
      TMS Software Blog
    • Delphi: So XOAuth2 SASL component that can do OAuth2 (via: Asbjørn Heid, G+) August 29, 2014
      Thanks Asbjørn Heid for sharing this: I made this!  - So this week I’ve been mostly trying to add some GMail integration to our app. Google now requires OAuth2 authentication when using IMAP, unless you turn off some scary-sounding security setting on your account. While Indy‘s IMAP ...
      The Wiert Corner (Jeroen Pluimers)
    • Dear everyone: Please stop using $IFDEF VERXXX August 28, 2014
      I just checked out the source to a new project.  Not going to name names because what I’m discussing is a pretty universal problem.  This project had dependencies on several common Delphi open-source libraries, and it had a well-designed DPROJ file that got all the paths right and everything. ...
      TURBU Tech (Mason Wheeler)
    • RAD Studio Tech Preview Webinar on September 4 August 28, 2014
      Next thursday Embarcadero will host a tech preview webinar of the coming version of RAD Studio, Delphi, and C++Builder. If you want to know what's coming in the next version of our products, sign up now at  forms.embarcadero.com/RADXE7FirstLookWebinar . The webinar will show the new ...
      Marco's Tech Blog (marcocantu)
    • Delphi: always watch the compiler Warnings August 28, 2014
      Quiz questions: Does the below unit test succeed or fail? Why? procedure TestHResult.Test_EOleException; var OleException: EOleException; IResult: LongInt; // == HResult UResult: Cardinal; // == DWord begin IResult := $800A11FD; UResult := $800A11FD; try OleException := ...
      The Wiert Corner (Jeroen Pluimers)
    • A way for a VCL TFrame to be notified when it is actually shown (via G+) August 27, 2014
      Interesting, as I didn’t think this was possible. Thanks Oliver Funcke! Notification when a `TFrame` becomes visible. TMyCommonFrame = class(TFrame) private FOnShow: TNotifyEvent; procedure CMShowingChanged(var M: TMessage); message CM_SHOWINGCHANGED; public property OnShow : ...
      The Wiert Corner (Jeroen Pluimers)
    • Some notes on Word automation and showing Bookmarks August 27, 2014
      Though I’ve done this automation in Delphi, this applies to automation from any development platform. In this particular project, the documents used Bookmarks. Those have changed over time, so has the Word support for it. From Word 2000 until Word you could disable the showing of Bookmarks by ...
      The Wiert Corner (Jeroen Pluimers)
    • Don't miss the RAD Studio US and Canada Tour – See it in person or online August 27, 2014
      You’re invited to join Embarcadero at a live in person or online event to see what’s coming in RAD Studio, Delphi and C++Builder.Experience RAD Studio live in New York, Montreal, Santa Monica, Vancouver or Toronto If you are near one of these cities or will be in the area between September 9-11, ...
      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: