2017-06-20 88 views
-11

我在Delphi 7中使用的Dropbox API V2 Windows桌面軟件開發。 我的代碼不起作用。請爲我提供一個正確的Delphi代碼! 謝謝!德爾福的Dropbox API第2版文件上傳

我的代碼片段:

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的服務器總是說:

400錯誤的請求,源文件是存在的。

請幫幫忙!

+1

請妥善格式化你的代碼。明確提出代碼和預期行爲。目前你的代碼很難解釋。 –

+0

400錯誤請求錯誤意味着HTTP後以某種方式畸形,所述服務器不接受。 – Fero

+0

是不是你的'(Dropbox的-API精氨酸:..'缺少一個右 ')' ** **裏面的字符串?實際上,關閉')'看起來像一個語法錯誤。 – MartynA

回答

1

您將請求發送到錯誤的網址,你正在濫用Request.CustomHeaders屬性,你malforming您的JSON數據。

嘗試sonething更像這個:

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;