Einzelnen Beitrag anzeigen

MartinK

Registriert seit: 21. Jun 2009
Ort: Germering (Germany)
89 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#15

AW: Daten abholen von einem CakePHP Server

  Alt 10. Okt 2014, 10:09
OK, here u go mit meinem Code zum Login

1.) wir erstellen uns einen record der die Anmeldedaten&Status beinhaltet um nachher einfacher darauf zugreifen zu können

Delphi-Quellcode:
  TaCloudLogin = record
     id :String; //returns the UID (User-ID) of the aCloud
     Username :String;
     Password :String;
     LogonSuccesfull :Boolean;
     PremiumStatus :Boolean; //does user get aditional access to functions like WebInterface etc. ?
   end;


Var
    aCloudLogin :TaCloudLogin;
2.) Die Funktion die den Login checkt und den record mit dem Anmeldedaten ausfüllt

Delphi-Quellcode:

Function TForm1.Login2ACloud(aUsername,aPassword,aLocale,anActiveVersion :String; aPremiumStatus:Integer; IsSilentMode:Boolean):Boolean;
Var
  iDHTTP: TIdHttp;
  Params: TStringStream;
  JSON: ISuperObject;
begin
  if NOT ((Length(EaCloudUsername.Text) >= 5) AND (Pos('@', EaCloudUsername.Text) > 1 ))
    then begin
           Showmessage(Txt[715,L]); //invalid e-mail adress
           exit;
         end;

  IF (Length(EaCloudPassword.Text) < 4)
    then begin
           Showmessage(Txt[718,L]); //password missing or invalid
           exit;
         end;

  IdHttp := TIdHttp.Create;
  Params := TStringStream.create('');

  //1.) Prepare and send a HTTP-Post to the CloudServer asking for LoggingIn
  try
    JSON := SO();
    JSON['UID'] := SO('"' + CreateGuid + '"');
    JSON['activeversion'] := SO(anActiveVersion);
    JSON['locale'] := SO(aLocale);
    JSON['password'] := SO(aPassword);
    JSON['premium'] := SO(aPremiumStatus);
    JSON['username'] := SO(aUsername);

    JSONParams := JSON.AsJSon();
    Params.WriteString(JSONParams);

    iDHTTP.HandleRedirects := True;
    iDHTTP.Request.BasicAuthentication := True;
    iDHTTP.Request.Authentication := TIdBasicAuthentication.Create;
    iDHTTP.Request.Authentication.Username := EaCloudUsername.Text;
    iDHTTP.Request.Authentication.Password := EaCloudPassword.Text;
    IdHTTP.Request.ContentType := 'application/json';
    IdHTTP.Response.KeepAlive := False;

    try
      JSONResponse := IdHTTP.Post(aCloudServer + 'users/login.json', Params);
    except
      on E: Exception do
        IF (IsSilentMode = FALSE)
          then showmessage('Error encountered during POST: ' + E.Message);
    end;
  finally
    IdHTTP.Free;
  end;

  //2.) Now we interprete if the connection was succesful and write data into the record "aCloudLogin"
  JSON := SO(JSONResponse); // Interprete the JSON response to the JSON Super-Object (SO)

  aCloudLogin.id := JSON.O['return'].S['id'];
  if (POS('OK', Uppercase(JSON.O['return'].S['response']) ) > 0)
    then aCloudLogin.LogonSuccesfull := True
    else aCloudLogin.LogonSuccesfull := FALSE;
  If (aPremiumStatus = 0)
    then aCloudLogin.PremiumStatus := True
    else aCloudLogin.PremiumStatus := FALSE;
  aCloudLogin.Username := aUsername;
  aCloudLogin.Password := aPassword;

  Result:= aCloudLogin.LogonSuccesfull;
end;
und hier noch ein Funktionsaufruf.....

  Login2ACloud(EaCloudUsername.Text,EaCloudPassword.Text,'de', 'Aqua Calculator (V' + LVersionNr.Caption + ')', 1, True);
lG Martin
Martin Kuhn
  Mit Zitat antworten Zitat