Thema: oAuth

Einzelnen Beitrag anzeigen

trax17

Registriert seit: 14. Jul 2011
2 Beiträge
 
#3

AW: oAuth

  Alt 15. Jul 2011, 07:40
Anbei mein Code.
Ich bin mir auch unsicher, da in der .Net Anwendung http://sharpbox.codeplex.com/ die Authentifizierung nicht komplett über oAuth abläuft:
https://www.dropbox.com/developers/docs#token

Mein Projekt ist als ZIP File im Anhang.


Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
  StdCtrls, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    edtAppKey: TEdit;
    Label2: TLabel;
    edtAppSecret: TEdit;
    Label3: TLabel;
    Label4: TLabel;
    edtUserName: TEdit;
    edtPassword: TEdit;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation


uses
OAuth;

{$R *.dfm}

var
ConsumerKey, ConsumerSecret, UserName, Password : String;
AccessTokenUrl, AuthorizationTokenUrl, RequestTokenUrl : String;

  IdHTTP1 : TIdHTTP;

  Consumer : TOAuthConsumer;
  ARequest : TOAuthRequest;
  Token : TOAuthToken;
  Response : String;
  HMAC : TOAuthSignatureMethod_HMAC_SHA1;
  TokenKey : String;
  TokenSecret : String;



procedure TForm1.btn1Click(Sender: TObject);
var
  Url : String;
begin

   AccessTokenUrl := 'http://api.dropbox.com/0/oauth/access_token';
   AuthorizationTokenUrl := 'http://www.dropbox.com/0/oauth/authorize';
   RequestTokenUrl := 'http://api.dropbox.com/0/oauth/request_token';

   ConsumerKey := edtAppKey.Text;
   ConsumerSecret := edtAppSecret.Text;
   UserName := edtUserName.Text;
   Password := edtPassword.Text;

   Consumer := TOAuthConsumer.Create(ConsumerKey, ConsumerSecret);
   HMAC := TOAuthSignatureMethod_HMAC_SHA1.Create;

   //Step 1 – Request request token (step A in diagram)

    IdHTTP1 := TidHTTP.create(nil);

    ARequest := TOAuthRequest.Create(RequestTokenUrl);
    ARequest := ARequest.FromConsumerAndToken(Consumer, nil, RequestTokenUrl);

    ARequest.Sign_Request(HMAC, Consumer, nil);

    Url := RequestTokenUrl + '?' + ARequest.GetString;

    Response := IdHTTP1.Get(Url);

    TokenSecret := Copy(Response, 20, 15);
    TokenKey := Copy(Response, 48, 63);


    Token := TOAuthToken.Create(TokenKey, TokenSecret);

    //Step 2 – Authorize (Step C in diagram)

    Url := AuthorizationTokenUrl;

    Url :=
    URL + '?' + 'oauth_token=' + TokenKey;

    WebBrowser1.Navigate(Url);

   end;




procedure TForm1.btn2Click(Sender: TObject);
var Url : String;
begin
    //Step 3 – Request access token (Step E in diagram)

    Url := '';
    Url := AccessTokenUrl;

    Consumer := nil;

    Consumer := TOAuthConsumer.Create(TokenKey, TokenSecret);

    ARequest.HTTPURL := Url;
    ARequest := ARequest.FromConsumerAndToken(Consumer, Token, URL);
    ARequest.Sign_Request(HMAC, Consumer, Token);

    Url := Url + '?' + ARequest.GetString;

    Response := idHTTP1.Get(Url);

    idhttp1.ResponseText;

    TokenSecret := Copy(Response, 20, 15);
    TokenKey := Copy(Response, 48, 63);

    Token := TOAuthToken.Create(TokenKey, TokenSecret);
end;

end.

Dazu verwende ich "OAuth for Delphi", zu finden unter: http://sourceforge.net/projects/oauthdelphi/
Angehängte Dateien
Dateityp: zip DropBox.zip (21,8 KB, 135x aufgerufen)
  Mit Zitat antworten Zitat