Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Looking for more examples of Unicode/Ansi oddities in Delphi 2009+ September 25, 2017
      At the end of April 2014, Roman Yankovsky started a nice discussion on Google+ trying to get upvotes for QualityCentral Report #:  124402: Compiler bug when comparing chars. His report basically comes down to that when using Ansi character literals like #255, the compiler treats them as single-byte ...
      The Wiert Corner (jpluimers)
    • Make your Delphi applications pop with Font Awesome! September 24, 2017
      Yes you heard right, Font Awesome? You can use their icons to make your desktop applications pop. Nowadays I use it to make my websites look nicer and without having to worry about finding icons for my apps and edit them and so on. Font Awesome is one the smartest things you can use to make your ...
      Random thoughts on coding (Jordi Corbilla)
    • Dependency Analysis – Pascal Today September 20, 2017
      Tools and steps for analysing Delphi or FreePascal code: Dependency Analysis – Pascal Today [WayBack] Used tools: Delphi Unit Dependency Scanner [WayBack] Gephi – The Open Graph Viz Platform [WayBack] yEd – Graph Editor [WayBack] yEd is a free desktop application to quickly create, ...
      The Wiert Corner (jpluimers)
    • Bugfix for DprojNormalizer available September 19, 2017
      There is a new version V2.1.0 for DprojNormalizer available, which targets a problem when using option sets and/or build events. Thanks to Peter Sonderegger for bringing this to my attention. Be aware that this version may change the sorting of your dproj files triggering a version control change. ...
      The Art of Delphi Programming (Uwe Raabe)
    • Delphi and Linux September 19, 2017
      I'd just like to remind my Slovenian readers that on 28th this month I'll be having a presentation about RAD Studio and Linux in Ljubljana.As the presentation will be given in Slovenian language, the rest of my post containing the description of the presentation is written in that language, ...
      The Delphi Geek (Primož Gabrijelčič)
    • delphi – Is AtomicCmpExchange reliable on all platforms? – Stack Overflow September 19, 2017
      TL;DR: yes it is. Answer by Allen Bauer at delphi – Is AtomicCmpExchange reliable on all platforms? – Stack Overflow [WayBack] –jeroenFiled under: Delphi, Development, Software Development
      The Wiert Corner (jpluimers)
    • Checking KeyPress is not the place to do your input validation September 19, 2017
      I have seen too many projects over the years trying to do input validation by checking KeyPress. This is not limited to Delphi projects (C#, VB and other projects suffer from this just as much). Most of these projects suffer from these:: Much of the KeyPress logic logic in the UI byusing half-baked ...
      The Wiert Corner (jpluimers)
    • Delphi and RAD Studio Roadmap Update Publsihed September 18, 2017
      The article with the new roadmap slides is available at: https://community.embarcadero.com/article/16563-rad-studio-roadmap-september-2017 The roadmap is updated with the 10.2.1 actual release and the specific 10.2.2 plans, along with the focus areas for 10.3 next year.
      Marco's Tech Blog (marcocantu)
    • Visiting Delphi Conference in Poland Next Week September 14, 2017
      On September 21st and 22nd (next Thursday and Friday) I'll be attending and giving sessions at a Delphi conference in Poland. The conference is organized by the local Embarcadero partner, BSC Polska. You can find more information at the "Zlot Programistow Delphi" web site, ...
      Marco's Tech Blog (marcocantu)
    • GetIt Content Update, September 2017 September 13, 2017
      We keep adding a lot of open source libraries, trial components, and now also demos and database connectors to the GetIt package manager. As I wrote a couple of months back, we added also a lot of new categories to simplify navigating among the 241 packages listed today. What was added over ...
      Marco's Tech Blog (marcocantu)

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/.

Advertisements

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

 
%d bloggers like this: