Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Hello Moto 360 from Delphi XE7 September 19, 2014
      I really like my Moto 360 watch. It looks fabulous, and does great as an extension of my Android phone, but of course the most important question is how to make an app for it. Turns out it just works with RAD Studio X7, Delphi or C++. Thanks to the new FireUI Multi-Device designer I can actually ...
      The PodCast at Delphi.org (Jim McKeeth)
    • Delphi XE7 Moto 360 Support w/FireUI September 19, 2014
      Jim Mckeeth demos Moto 360 support for Delphi XE7. Jim is using XE7’s new FireUI and the new Moto 360 circular FireUI view (available soon as a free XE7 download) to build and deploy a native Moto 360 app to the watch. Demos for C++ and Appmethod also soon. Developers without access to a Moto ...
      Michael Swindell (Michael Swindell)
    • Castalia IDE plug-in is part of the XE7 Bonus Pack September 19, 2014
      Castalia is an amazing IDE productivity tool. If you haven’t checked it out, you should. And now you can as part or your RAD Studio or Delphi XE7 purchase by September 30th. Marco shares some excitement about it on his blog: ...
      JT @ Embarcadero (J T)
    • Free Castalia for Delphi XE7 September 19, 2014
      Castalia is a great addition to the Delphi IDE and Embarcadero offers a free license of Castalia to all XE7 developers. That's a great offer. If you are already on XE7, you an download your copy of Castalia from cc.embarcadero.com/item/29995. If not, what are you waiting? Buy before the end of ...
      Marco's Tech Blog (marcocantu)
    • Users Like FireUI and Dynamic Arrays September 18, 2014
      A snapshot from the XE7 presentation in Ljubljana. Highlights of the presentation were FireUI, System.Threading and dynamic arrays. ‘Twas a very nice crowd, asking LOTS of questions. It was also good to see some young(er) people and some Delphi [insert a prehistoric version here] users who finally ...
      The Delphi Geek (gabr)
    • Dynamic Arrays in RAD Studio XE7 September 18, 2014
      Marco discusses a significant new language feature for Dynamic Arrays. http://blog.marcocantu.com/blog/2014_september_dynamic_arrays_delphixe7.html Share This | Email this page to a friend
      JT @ Embarcadero (J T)
    • Dynamic Arrays in Delphi XE7 September 18, 2014
      There is a significant new feature in the Object Pascal language for XE7 and that is the improved support for initializing dynamic arrays and operating on them. The What's new page covers is here: docwiki.embarcadero.com/RADStudio/XE7/en/What%27s_New_in_Delphi_and_C%2B%2BBuilder_XE7#String- ...
      Marco's Tech Blog (marcocantu)
    • Delphi, C++Builder and RAD Studio XE7 first impressions from real developers September 18, 2014
      I recently did a Survey Monkey asking for first impressions from some of the first Delphi, C++Builder and RAD Studio XE7 customers that appeared in our registration database.  Here are just a few of the many responses that we received in the first few hours. FireUI "The XE7 FireUI Multi-Device ...
      Sip from the Firehose (David Intersimone)
    • DevExpress announces VCL components v14.1.4 now supports XE7 September 18, 2014
      DevExpress VCL component pack version 14.1.4 is now compatible with Delphi, C++Builder and RAD Studio XE7. DevExpress CTO Julian Bucknall posted a blog entry on September 9 announcing the release with XE7 support. Julian said: "I am happy to announce that we released DevExpress VCL v14.1.4 early ...
      Sip from the Firehose (David Intersimone)
    • Internet of Things using Bluetooth with David I - Developer Skill Sprint - Sept 18, 2014 September 18, 2014
      Today’s developer skill sprint is titled "Internet of Things with Bluetooth". I have uploaded the slides (slightly modified from the presentation) to Embarcadero’s Slideshare site at http://www.slideshare.net/embarcaderotechnet/internet-of-things-xe7-bluetooth-skillsprintdavidi. The ...
      Sip from the Firehose (David Intersimone)

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: