7

我尝试将文件上传到保管箱。
我使用 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;

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

4

2 回答 2

8

试试这个:

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;
于 2013-01-29T18:32:02.580 回答
0
  1. 在参数%26中使用。通过符号连接的一个参数中有两个值。&oauth_signature&
  2. 通过TMemoryStream.

    procedure TDropbox.Upload(const AFileName: String);
    const
      API_URL = 'https://api-content.dropbox.com/1/files_put/sandbox/';
    var
      URL: String;
      Stream: TMemoryStream;
      ShortFileName: String;
      https: TIdHTTP;
      SslIoHandler: TIdSSLIOHandlerSocket;
    begin
      if not FileExists(AFileName) then
      begin
        raise EInOutError.CreateFmt('File %s not found', [AFileName]);
      end;
    
      ShortFileName := ExtractFileName(AFileName);
      URL := API_URL+ShortFileName
        + '?oauth_signature_method=PLAINTEXT&oauth_consumer_key=' + FAppKey
        + '&oauth_token=' + FOAuth.AccessToken
        + '&oauth_signature=' + FAppSecret + '%26' + FOAuth.AccessTokenSecret;
    
      https := TIdHTTP.Create(nil);
      Stream := TMemoryStream.Create;
      try
        SslIoHandler := TIdSSLIOHandlerSocket.Create(https);
        SslIoHandler.SSLOptions.Method := sslvTLSv1;
        SslIoHandler.SSLOptions.Mode := sslmUnassigned;
    
        https.IOHandler := SslIoHandler;
        Stream.LoadFromFile(AFileName);
    
        https.Post(URL, Stream);
      finally
        FreeAndNil(Stream);
        FreeAndNil(https);
      end;
    end;
    
于 2013-01-29T11:46:06.267 回答