AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Daten abholen von einem CakePHP Server

Daten abholen von einem CakePHP Server

Ein Thema von MartinK · begonnen am 4. Okt 2014 · letzter Beitrag vom 8. Jan 2015
Antwort Antwort
Seite 2 von 2     12
mjustin

Registriert seit: 14. Apr 2008
3.003 Beiträge
 
Delphi 2009 Professional
 
#11

AW: Daten abholen von einem CakePHP Server

  Alt 9. Okt 2014, 14:15

Login ausführen durch:
http://aServerName.de/users/login.json

Hierzu muss ein HTTPBody Field mitgeschickt werden (Anmerkung: was auch immer das genau ist...)

HTTPBody
{
UID = "04DAB959-9EF6-442C-8A19-D463C57E61D5";
activeversion = 312;
locale = de;
password = aPassword;
premium = 0;
username = "its.me@mail.com";
}
Das ist kein gültiges JSON. Steht das genau so in der Dokumentation?

Wenn es JSON sein soll, das im Body gesendet wird, dann empfehle ich einen JSON Parser wie SuperObject zu verwenden.
Michael Justin
habarisoft.com
  Mit Zitat antworten Zitat
Benutzerbild von Valle
Valle

Registriert seit: 26. Dez 2005
Ort: Karlsruhe
1.223 Beiträge
 
#12

AW: Daten abholen von einem CakePHP Server

  Alt 9. Okt 2014, 14:17
Das ist kein gültiges JSON. Steht das genau so in der Dokumentation?
Was er schickt ist auch kein JSON.

Daher die Frage, was er überhaupt senden soll. Fest steht nur, dass er vermutlich JSON zurückbekommt.
Valentin Voigt
BOFH excuse #423: „It's not RFC-822 compliant.“
Mein total langweiliger Blog
  Mit Zitat antworten Zitat
mjustin

Registriert seit: 14. Apr 2008
3.003 Beiträge
 
Delphi 2009 Professional
 
#13

AW: Daten abholen von einem CakePHP Server

  Alt 9. Okt 2014, 14:20

Delphi-Quellcode:
  Params := TStringStream.create('');
  try
    Params.WriteString(URLEncode('UID='           + '04DAB959-9EF6-442C-8A19-D463C57E61D5' + '&'));
Das Ergebnis in Params passt nicht zum dargestellten Format (& statt .

Tipp: für das Debuggen kann man ganz einfach einen Interceptor in Indy zuweisen, der dann die Kommunikation protokolliert. Wenn das zu einfach ist ( ) kann man alternativ Fiddler2 als Proxy zum Loggen einsetzen.
Michael Justin
habarisoft.com
  Mit Zitat antworten Zitat
MartinK

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

AW: Daten abholen von einem CakePHP Server

  Alt 9. Okt 2014, 19:04
Ich würde mal sagen die Doku der API die ich erhalten habe ist noch "sehr basic". Die muss und werde ich im Anschluss selbst überarbeiten.

Die Idee mit dem JSON Superobjekt war mal wieder der Bringer überhaupt.
Das Teil ist ja echt Weltklasse sobald man es mal halbwegs verstanden hat.

-> Der Übergabe HTTPBody ist JSON
-> und ich bekomme inzw. auch einen passenden JSON String als response zurück, welchen ich nun nur noch auswerten muss

Bin absolut begeistert wie Klasse das funktioniert, muss jetzt aber erstmal etwas essen bevor ich das fertig mache.
Ich poste dann vermutlich wieder meinen Code, damit andere auch etwas davon haben

VIELEN DANK euch allen erneut
LG MARTIN
Martin Kuhn
  Mit Zitat antworten Zitat
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, 11: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
MartinK

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

AW: Daten abholen von einem CakePHP Server

  Alt 29. Okt 2014, 16:27
Nachdem jetzt mit direktem www Zugang alles klappt, habe ich noch ein Thema sobald ich via Proxy verbinde

hier mal ein Stück Code das bei mir heraussucht welche Art von Proxy-Auth gemacht wird
Delphi-Quellcode:
procedure TForm1.IdHTTPProxyAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin
  Handled := False;
end;

function TForm1.IdSSLIOHandlerSocketOpenSSLVerifyPeer(Certificate: TIdX509; AOk: Boolean; ADepth, AError: Integer): Boolean;
begin
  Result := True;
end;


procedure TForm1.IdHTTPSelectProxyAuthorization(Sender: TObject; var AuthenticationClass: TIdAuthenticationClass; AuthInfo: TIdHeaderList);

begin
  ProxyAuthTxt := 'Proxy-Authentification: Unkown';

  // First check for NTLM authentication, as you do not need to set username and password because Indy will automatically
  // handle passing your Windows Domain username and password to the proxy server
  if (pos('Proxy-Authenticate: NTLM', IdHTTP.Response.RawHeaders.Text)>0)
    then begin
           IdHTTP.ProxyParams.BasicAuthentication := false;
           AuthenticationClass := TIdSSPINTLMAuthentication;
           ProxyAuthTxt := 'Proxy-Authentific.: NTML (w/o Username+PW)';
           ProxyAuthType := 1;
         end
    else begin
           //...now check for Basic Authentication
           if (pos('Proxy-Authenticate: Basic', IdHTTP.Response.RawHeaders.Text)>0)
             then begin
                    IdHTTP.ProxyParams.BasicAuthentication := true;
                    AuthenticationClass := TIdBasicAuthentication;
                    ProxyAuthTxt := 'Proxy-Authentification: Basic';
                    ProxyAuthType := 2;
                   end
             else begin
                    // Then Digest
                    if (pos('Proxy-Authenticate: Digest', IdHTTP.Response.RawHeaders.Text)>0)
                      then begin
                             IdHTTP.ProxyParams.BasicAuthentication := true;
                             AuthenticationClass := TIdDigestAuthentication;
                             ProxyAuthTxt := 'Proxy-Authentification: Digest';
                             ProxyAuthType := 3;
                           end;
                  end;
           //.------------
           IdHTTP.ProxyParams.ProxyUsername := EProxyUsername.Text;
           IdHTTP.ProxyParams.ProxyPassword := EProxyPassword.Text;
         end;

  LProxyAutent.Caption := ProxyAuthTxt;
end;

HTTP Get funktioniert auch einwandfrei solange ich ein normales get/Post mache,
"OHNE dem Server ein Username/Password für den Cloud-Zugriff mitzuübergeben"
(URL, Port und Pasword/Username wegen das proxys sind natürlcih angegeben passen !)


sobald ich den Cloud Username und Passwort mit angebe, bekomme ich beim "IdHttp.Get" eine Exception "HTTP/1.1 400 bad request"
Gehe ich ich aber nicht über den Proxy, sondern verbinde direkt, dann funktioniert der Code eiwandfrei;

Delphi-Quellcode:
Function TForm1.Ask4aCloudResponse(aUsername, aPassword, aServerURL, aServerCommand: String ):String;
begin
  try
    IdHTTP.HandleRedirects := True;

    iDHTTP.Request.BasicAuthentication := True;
    iDHTTP.Request.Authentication := TIdBasicAuthentication.Create;
    iDHTTP.Request.Authentication.Username := aUsername;
    iDHTTP.Request.Authentication.Password := aPassword;
    IdHTTP.Request.ContentType := 'application/json';
    Result := IdHttp.Get(aServerURL + aServerCommand);
  finally
    ;
  end;
end;
woran kann denn das liegen?
Martin Kuhn
  Mit Zitat antworten Zitat
MartinK

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

AW: Daten abholen von einem CakePHP Server

  Alt 5. Nov 2014, 14:56
Einige Tage später und "der Knoten hat sich leider noch nicht gelöst"

Ich denke mein Problem ist das ich 2 Pärchen aus Username & Password habe
a) Username & Password die ich brauche um mich bei einem "Basic" oder "Digest" Proxy zu authentifizieren
(das müssten doch eigentlich sein: IdHTTP.ProxyParams.ProxyUsername / ProxyPassword
wobei die ja zumindest bei NTML ja irgendwie duch Indy eingesetzt werden)

b) Username & Password die dem Cloud-Server übergeben werden müssen um sich für den eigenen Cloud-Acount zu indetifizieren
(das müssten doch eigentlich sein: iDHTTP.Request.Authentication.Username / Password)

hat da jemand noch eine Idee die mich aus der völligen Desillusion befreit ?

LG Martin
Martin Kuhn
  Mit Zitat antworten Zitat
MartinK

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

AW: Daten abholen von einem CakePHP Server

  Alt 19. Nov 2014, 14:18
Manchmal braucht man leider Ewigkeiten für eine Kleinigkeit.. hier war das der Fall

Ich habe den Fehler nach mehrfachem Kontakt zu Remy lebau (von Indy) und mittels 2 WireShark-Protokollen herausfinden können.
Und zwar habe ich 1x das Protokoll meiner App gelogged und das mit dem Protokoll des IE verglichen (wo das ganze funktioniert hatte...)

Mein Fehler war dass ich beim Übergabe-String so etwas geschickt hatte
'measurements/changed.json?date=1970-01-01 01:01:00' der MS-IE hat das Kommando intern umgewandelt in
'measurements/changed.json?date=1970-01-01%2001:01:00' ...und damit lief es

Die Lösung ist also nichts weiter als ein
URLEncode('tanks/changed.json?date=1970-01-01 00:00:00') und alles ist gut


LG Martin
Martin Kuhn
  Mit Zitat antworten Zitat
MartinK

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

AW: Daten abholen von einem CakePHP Server

  Alt 8. Jan 2015, 11:47
Zwecks Vollständigekit halber hier ein weiteres CodeFragment, da ich denke das es evtl. weiteren Personen hilft die mal etwas ähnliches machen wollen

Ziel: Ein FileUpload auf den Server mittels eines sog "Multipart Form DataStreams". So etwas wird zB zum Uploaden von Bildern eingesetzt

lG Martin

Delphi-Quellcode:
Function TForm1.ACloud_UploadPicture(_PictureID, _OriPictureFPathAndName:String) : String;
Var
  POSTData: TIdMultipartFormDataStream;
  JSON: ISuperObject;
begin
  Result := '';
  try
    //generate a so called Multipart Object with
    //- the picture File itself. it is loaded as Stream form the path specified
    //- identifiers needed for the Cake server routines 'picture' , 'image/jpg'
    POSTData := TIdMultiPartFormDataStream.Create;
    POSTData .AddFile('picture', _OriPictureFPathAndName, 'image/jpg');

    IDHTTP.HandleRedirects := True;
    IDHTTP.Request.BasicAuthentication := false;
    IDHTTP.Request.Authentication := TIdBasicAuthentication.Create;
    IDHTTP.Request.Authentication.Username := aCloudLogin.Username;
    IDHTTP.Request.Authentication.Password := aCloudLogin.Password;
    //although we handle the Multipart message, servers answers are again JSON for easier interpretation
    IdHTTP.Request.ContentType := 'application/json';

    aCloudURL := URLEncode(aCloudServer + 'pictures/uplpic.json?PictureID=' + _PictureID+'.jpg');
    JSONResponse := IDHTTP.Post(aCloudURL, POSTData);
  except
    on E : Exception do
      Result := Result + 'Exception uploading an image to aCloud: "' + E.ClassName + '" "' + E.Message +'"';
  end;
  POSTData.Free;
  //---------------------------
  if Result <> 'then exit;
  //---------------------------
  //Now let's check if everythimng worked correctly. ...we interprte the Response returend from the Server
  //This is specific to your servers specifications and just an example on how it could be done
  JSON := SO();
  JSON := SO(JSONResponse); // Interprete the response to the JSON Super-Object (SO)
  If (Uppercase(JSON.S['response']) <> Uppercase('OK') ) OR
     (Uppercase(JSON.S['entityID']) <> Uppercase(_PictureID+'.jpg')) OR
     (Uppercase(JSON.S['entityType']) <> Uppercase('picturedata') ) OR
     (Uppercase(JSON.S['type']) <> Uppercase('u') )
     then Result := Result + 'Error responded updating an aCloud-picture: ' + JSONResponse;
end;
Martin Kuhn
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:38 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz