Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Biotopia October 18, 2014
      "Biotopia" is our new project from Magic Storm, our hobby game development team. It is a mixture of RPG, Jump & Run Platformer and most of all a sandbox, where the Player can let his fantasy flow. The game is developed with english and german language support, if the community helps us we like ...
      Pascal Game Development
    • Windows Gets Docker October 18, 2014
      As I mentioned before, I really believe Docker would help the windows ecosystem. All my clients end up having loads of server boxes, doing nothing, with 1 app per box, just to keep the isolated so nothing breaks something else. Docker gets all the advantages of virtual machines, within out the ...
      David Glassborow
    • Reserve your space: CodeRage 9 - the Delphi developer event of the year October 17, 2014
      CodeRage 9 - The totally technical online conference for Delphi, C++Builder and RAD Studio dedvelopers is just around the corner. The free online conference will be taking place October 28-30.Join us for CodeRage® 9, the totally free, totally technical online conference for software ...
      Delphi Insider (Tim Del Chiaro)
    • 2014 Conference in Brazil October 17, 2014
      Yesterday in Sao Paulo myself and Jim McKeeth gave session at the largest Delphi event of the year, a great gathering of passionate developers. With over 600 Delphi developers attending this was really a big event, with a perfect organization, and many great speakers, mostly local Delphi MVPs who ...
      Marco's Tech Blog (marcocantu)
    • Delphi development update October 17, 2014
      Hi everyone, I can't believe it's been over a year since my last posting. My last post was having a moan about the cost of Delphi XE5, coming from XE4. Well, as many pointed out to me, the Software Assurance plan was the way to go and I'm now happily running on Delphi XE7 and enjoying being on the ...
      The Delphi Disciple (Jason Sweby)
    • Delphi XE7 and iOS error “Unable to mount developer image (e800000e)” October 17, 2014
      Stumbled across this error during Delphi XE7 iOS deployment: Unable to mount developer image (e800000e). Solution: reinstall Xcode command line tools with xcode-select --install A software update popup window will appear, click Install, agree to the Terms of Service and wait for the Command Line ...
      The curse of Dennis D. Spreen (Dennis)
    • The Dreaded Black Burger of Tokyo October 17, 2014
      I’m pretty strict about keeping this blog developer-related, so please forgive me this one off-topic post. I’ve been in Tokyo this week for work, and on Monday I was walking passed a McDonald’s in Ochanomizu when I was struck by the following poster out the front. My first reaction ...
      Malcolm Groves (Malcolm Groves)
    • Will you help me? I need two Delphi developers to work for me in Hoover, AL. October 16, 2014
      Any help spreading this message to the Birmingham, AL area would be most appreciated! :)Work with awesome people in a great area!Email me and I'll hook you up!  SOFTWARE DEVELOPER  –  DELPHI – Hoover, ...
      The Delphi Hacker's Corner (Anders Ohlsson)
    • Do you love Delphi? How about Paris? Work for WideOrbit in Paris! October 16, 2014
      This is a fantastic opportunity to work for a US based company in the best city in the world - Paris, France!Send me an email and I will get you to the right people right away. Thanks!WideOrbit– Software Developer - Delphi – Paris, France About WideOrbit: WideOrbit is the leading provider of ...
      The Delphi Hacker's Corner (Anders Ohlsson)
    • CodeRage 9 is happening October 28-30, 2014 October 16, 2014
      Register for CodeRage 9 - October 28-30, 2014Join everyone at Embarcadero for CodeRage® 9, the totally free, totally technical online conference for software developers. This year's conference brings you top industry speakers and technologists presenting on a wide variety of topics, all geared ...
      The Delphi Hacker's Corner (Anders Ohlsson)

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: