如何通过 Delphi 7 将文件上传到 Dropbox? [英] how to upload file to dropbox via Delphi 7?

查看:29
本文介绍了如何通过 Delphi 7 将文件上传到 Dropbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将文件上传到 Dropbox.
我使用 dropbox api https://www.dropbox.com/developers/reference/api#files-POST

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:	est.txt', 'application/octet-stream');

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

我收到400 错误请求".
所有令牌都是正确的(其他 api 运行良好).
这个api如何传递参数?

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

推荐答案

试试这个:

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;

这篇关于如何通过 Delphi 7 将文件上传到 Dropbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆