Ruminations of an addled mind

Random ramblings about my thoughts

  • Meta

  • RSS DelphiFeeds.com

    • Why I dislike GoToWebinar by Citrix: 0 stars out of 5. October 31, 2014
      Last week, I viewed to webinars. A smaller one Geek Fest – Automating the ForgeRock Platform Installation about Ansible installation of Unix machines, and the first 70% of the last day ofCodeRage 9 | Free Development Event by Embarcadero. Both had a bad experience because of GoToWebinar has a ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: Creating Beautiful Animations in your Desktop and Mobile Applications (Boian Mitov) October 30, 2014
      Session: Creating Beautiful Animations in your Desktop and Mobile Applications The session demonstrates how to create beautiful timeline-based animations in Object Pascal using the Mitov Software’s AnimationLab. Level: All Boian Mitov – Mitov Software Q&A: Welcome to CodeRage 9 ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: Advanced RTTI in Object Pascal (Ray Konopka) October 30, 2014
      Session: Advanced RTTI in Object Pascal Runtime Type Information RTTI has always been a core feature of Delphi. But until recently, it was complex and limited. Now, RTTI is significantly more powerful, and yet much easier to use as well. This session describes in detail all of the changes that have ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: Android Push Notifications (Jeff Lefebvre) October 30, 2014
      Downloads: CodeRage 9 Files! This year I presented Push notifications with Android. Here are the files mentioned in the session! Here is the android project: CodeRage Delphi Here is the TGCMReceiver Component and java file: TGCMReceiver And here is the post that helped me albeit out of date! Link ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: Understanding Distributed Version Control (Robert Love) October 30, 2014
      Session: Understanding Distributed Version Control This session helps developers who have been working with a Centralized Version Control System such as SVN to understand and use Distributed Version Control such as GIT or Mercurial. This session focuses primarily on GIT, but applies to Mercurial. ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: Bluetooth Classic and LE Programming with VCL and FMX (Diego Navarro) October 30, 2014
      Session: Bluetooth Classic and LE Programming with VCL and FMX Level: Intermediate Diego Navarro – Embarcadero Q&A: Audience Question Q: https://developer.bluetooth.org/TechnologyOverview/Pages/GATT.aspx A: Thx Audience Question Q: can you please paste links int the chat window A: ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: InterBase Keynote (Stephen Ball) October 30, 2014
      Session: InterBase Keynote If you need to manage distributed data over multiple locations then don’t miss the product address for InterBase! The next generation of InterBase is coming soon and will be super charged with a powerful new change tracking engine that makes managing distributed ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: Working with PDF and Office Documents using VCL and FireMonkey (Girish Patil) October 30, 2014
      Session: Working with PDF and Office Documents using VCL and FireMonkey In this session, we take a deep look into the all-new multi-format document processing toolset for Delphi from Gnostice. We take a brief look at architectural details of the product and then go into using the product. ...
      The Wiert Corner (jpluimers)
    • CodeRage 9 Q&A: NestedDataSets in ClientDataSets (Cary Jensen) October 30, 2014
      Session: NestedDataSets in ClientDataSets A column of a ClientDataSet can contain a dataset, a tabular structure containing one or more columns and zero or more records. This presentation continues Cary Jensen’s CodeRage series on ClientDataSets by looking at this power field type. Level: All ...
      The Wiert Corner (jpluimers)
    • New series! Developer Skill Sprints for Modernizing your VCL Apps October 30, 2014
      Developer Skill Sprints for Modernizing your VCL AppsNew Developer Skill Sprints starting on November 4!The next round of Developer Skill Sprints are here and focused on VCL application development! Join us for this 8-part series on modernizing and extending your VCL apps with RAD Studio XE7. We’ll ...
      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: