2017-06-20 4 views
-11

Ich entwickle in Delphi 7 eine Windows-Desktop-Software mit Dropbox API V2. Mein Code funktioniert nicht. Bitte geben Sie mir einen korrekten Delphi-Code! Danke!Delphi Dropbox API v2 Datei-Upload

Mein Code-Schnipsel:

procedure TDropbox.Upload(SourceFileName: String; DropBoxFileName: String; FTimeout: Integer = 5000); 
const 
    FDropBoxAppAccessToken = 'xxxxxxxxxxxxxxxxxxxx';  
var  
    FHTTPS: TIdHTTP;  
    FStream: TFileStream;  
    FDropBoxResponseCode: Integer;  
    FHTTPResponse: String;  
begin  
    FHTTPRequestURL := 'https://api-content.dropbox.com/2/files/upload';  
    FStream := TFileStream.Create(SourceFileName, fmOpenRead);  
    FStream.Position := 0;  
    FHTTPS := TIdHTTP.Create(nil);  
    FHTTPS.ConnectTimeout := FTimeout;  
    FDropBoxResponseCode := 0;  
    FHTTPResponse := '';  
    params := TStringList.Create;  
    arg := 'Dropbox-API-Arg:{"path:"' + FDropBoxBaseAppPath + DropBoxFileName + '}';  
    try  
    FHTTPS.Request.CustomHeaders.Add('Authorization:Bearer ' + FDropBoxAppAccessToken);  
    FHTTPS.Request.CustomHeaders.AddStrings := '(Dropbox-API-Arg:{"path:"' + FDropBoxBaseAppPath + DropBoxFileName + '}');  
    FHTTPS.Request.CustomHeaders.Values[arg];  
    FHTTPS.Request.ContentType := 'application/octet-stream';  
    FHTTPS.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(FHTTPS);  
    FHTTPResponse := FHTTPS.Post(FHTTPRequestURL, FStream);  
    except on E: Exception do  
    begin  
    end;  
    end; 

Dropbox-Server immer sagt:

400 Bad Anfrage, die Quelldatei vorhanden ist.

Bitte helfen!

+1

Formatieren Sie Ihren Code. Offensichtlich Code und erwartetes Verhalten. Derzeit ist Ihr Code schwer zu interpretieren. –

+0

400 Ungültiger Anforderungsfehler bedeutet, dass der HTTP-Post in einer Weise fehlerhaft ist, die der Server nicht akzeptiert. – Fero

+0

Ist nicht Ihre '(Dropbox-API-Arg: .. 'fehlt eine Schließung') '** innerhalb der Zeichenfolge? So wie es ist, das Schließen ')' sieht wie ein Syntaxfehler aus. – MartynA

Antwort

1

Sie senden die Anfrage an die falsche URL, Sie missbrauchen die Request.CustomHeaders Eigenschaft, und Sie machen Ihre JSON-Daten fehlerhaft.

Versuchen ähnliche stattdessen sonething: richtig

procedure TDropbox.Upload(const SourceFileName: String; const DropBoxFileName: String; Timeout: Integer = 5000); 
const 
    FDropBoxAppAccessToken = 'xxxxxxxxxxxxxxxxxxxx'; 
    FHTTPRequestURL := 'https://content.dropboxapi.com/2/files/upload'; 
var 
    FHTTPS: TIdHTTP; 
    FStream: TFileStream; 
    FDropBoxResponseCode: Integer; 
    FHTTPResponse: String; 
begin 
    FDropBoxResponseCode := 0; 
    try 
    FStream := TFileStream.Create(SourceFileName, fmOpenRead or fmShareDenyWrite); 
    try 
     FHTTPS := TIdHTTP.Create(nil); 
     try 
     FHTTPS.ConnectTimeout := Timeout; 
     FHTTPS.Request.CustomHeaders.Values['Authorization'] := 'Bearer ' + FDropBoxAppAccessToken; 
     FHTTPS.Request.CustomHeaders.Values['Dropbox-API-Arg'] := '{"path": "' + FDropBoxBaseAppPath + DropBoxFileName + '"}'; 
     FHTTPS.Request.ContentType := 'application/octet-stream'; 
     FHTTPS.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(FHTTPS); 
     FHTTPResponse := FHTTPS.Post(FHTTPRequestURL, FStream); 
     finally 
     FHTTPS.Free; 
     end; 
    finally 
     FStream.Free; 
    end; 
    except 
    on E: Exception do begin 
    end; 
    end; 
end;