Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to upload file to dropbox via Delphi 7?

I try to upload file into dropbox.
I use dropbox api https://www.dropbox.com/developers/reference/api#files-POST

procedure TDropbox.Upload2;
const
  URL = 'https://api-content.dropbox.com/1/files/dropbox/';
var
  Response: String;
  Params: TIdMultipartFormDataStream;
  https: TIdHTTP;
  SslIoHandler: TIdSSLIOHandlerSocket;
begin
  https := TIdHTTP.Create(nil);
  Params := TIdMultipartFormDataStream.Create();
  try
    SslIoHandler := TIdSSLIOHandlerSocket.Create(https);
    SslIoHandler.SSLOptions.Method := sslvTLSv1;
    SslIoHandler.SSLOptions.Mode := sslmUnassigned;

    https.IOHandler := SslIoHandler;

    Params.AddFormField('oauth_signature_method', 'PLAINTEXT');
    Params.AddFormField('oauth_consumer_key', FAppKey);
    Params.AddFormField('oauth_token', FOAuth.AccessToken);
    Params.AddFormField('oauth_signature', FAppSecret + '&' + FOAuth.AccessTokenSecret);

    Params.AddFile('file', 'C:\test.txt', 'application/octet-stream');

    https.Post(URL + 'test.txt', Params);
  finally
    FreeAndNil(https);
    FreeAndNil(Params);
  end;
end;

I got "400 Bad request".
All tokens are correct (other api works well).
How pass parameters for this api?

like image 285
barbaris Avatar asked Jan 29 '13 09:01

barbaris


1 Answers

Try this instead:

procedure TDropbox.Upload(const AFileName: String);
const
  API_URL = 'https://api-content.dropbox.com/1/files_put/sandbox/';
var
  URL: String;
  https: TIdHTTP;
  SslIoHandler: TIdSSLIOHandlerSocket;
begin
  URL := API_URL+ExtractFileName(AFileName)
    + '?oauth_signature_method=PLAINTEXT&oauth_consumer_key=' + FAppKey
    + '&oauth_token=' + FOAuth.AccessToken
    + '&oauth_signature=' + FAppSecret + '%26' + FOAuth.AccessTokenSecret;

  https := TIdHTTP.Create(nil);
  try
    SslIoHandler := TIdSSLIOHandlerSocket.Create(https);
    SslIoHandler.SSLOptions.Method := sslvTLSv1;
    SslIoHandler.SSLOptions.Mode := sslmUnassigned;

    https.IOHandler := SslIoHandler;
    https.Post(URL, AFileName);
  finally
    FreeAndNil(https);
  end;
end;
like image 98
Remy Lebeau Avatar answered Sep 20 '22 20:09

Remy Lebeau