2

我正在尝试在 Delphi XE2 中使用 Google Drive API,到目前为止,我几乎可以正常工作。我正在努力解决的一件事是分段上传。当我尝试上传文件时,我收到 503 错误。通常,这应该表明服务器有问题。但是,当我使用 fiddler 而不是我的 API 向同一 URL 发送具有相同正文和标头的上传请求时,文件已成功上传。这告诉我我的代码一定有问题。这个特殊的功能是一团糟,但在这里。

function TGoogleDriveApi.MultipartUpload(aStream : TStream; aTitle : string = 'Untitled';
  aDescription : string = ''; mimeType : string = 'application/octet-stream';
  indexableText : IGoogleDriveIndexableText = nil;
  lastViewedByMeDate : string = ''; modifiedDate : string = '';
  parents : IGoogleDriveParentList = nil) : IGoogleDriveFile;
var
  json, url, body, boundry, contentType : string;
  ss : TStringStream;
  ms : TMemoryStream;
  lHttpHelper : IHttpHelper;
  streamBytes : TBytes;
begin
  boundry := 'a_boundry';
  body := '--'+boundry+sLineBreak;
  body := body + 'Content-Type: application/json; charset=UTF-8';
  lHttpHelper := THttpHelper.Create;
  if(Token.IsExpired) then
    Token.Refresh(TokenUrl, OAuth.ClientId, OAuth.ClientSecret);
  url := 'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token='+lHttpHelper.UrlEncode(Token.access_token);
  json := '{';
  json := json + '"title": "'+aTitle+'"';
  if(aDescription <> '') then
    json := json + ', "description": "'+aDescription+'"';
  if(lastViewedByMeDate <> '') then
    json := json + ', "lastViewedByMeDate": "'+lastViewedByMeDate+'"';
  json := json + ', "mimeType": "'+mimeType+'"';
  if(modifiedDate <> '') then
    json := json + ', "modifiedDate": "'+modifiedDate+'"';
  json := json + '}';
  body := body + sLineBreak + sLineBreak + json + sLineBreak + sLineBreak + '--'+boundry;
  body := body + sLineBreak + 'Content-Type: '+mimeType + sLineBreak  + sLineBreak;
  body := body + 'some test text from the api' + sLineBreak + sLineBreak + '--'+boundry+'--';

  ss := TStringStream.Create;
  ss.WriteString(body);
  ss.Position := 0;
  ms := TMemoryStream.Create;
  ms.Write(ss,ss.Size);
  SetLength(streamBytes,aStream.Size);
  aStream.Read(streamBytes,aStream.Size);
  ms.Write(streamBytes[0],aStream.Size);
  ss.Clear;
  ss.Position := 0;
  ss.WriteString(sLineBreak + sLineBreak + '--'+boundry+'--');
  ss.Position := 0;
  ms.Write(ss,ss.Size);

  contentType := 'multipart/related; boundary="'+boundry+'"';
  json := lHttpHelper.PostResponse(url,contentType,ms);
  FreeAndNil(ss);
  FreeAndNil(ms);
  Result := nil;
end;

导致问题的线路是lHttpHelper.PostResponse呼叫。代码如下所示:

function THttpHelper.PostResponse(url, contentType : string; aStream : TStream) : string;
var
  lHTTP: TIdHTTP;
  lStream : TStringStream;
  handler : TIdSSLIOHandlerSocketOpenSSL;
begin
  lStream := TStringStream.Create;
  lHTTP := TIdHTTP.Create(nil);
  handler := TidSSLIOHandlerSocketOpenSSL.Create(nil);
  handler.SSLOptions.Method := sslvSSLv3;
  lHTTP.IOHandler := handler;
  try
    lHTTP.Request.ContentType := contentType;
    lHTTP.Post(url,aStream,lStream);
    lStream.Position := 0;
    Result := lStream.ReadString(lStream.Size);
      //except
  finally
    lStream.Free;
    lHTTP.Free;
    handler.Free;

    lStream := nil;
    lHTTP := nil;
    handler := nil;
  end;
end;

我目前正在MultipartUpload从我的测试中调用该函数,如图所示

procedure TestIGoogleDriveApi.TestMultipartUpload;
var
  ReturnValue : IGoogleDriveFile;
  fs : TFileStream;
begin
  fs := TFileStream.Create('testupload.jpg',fmOpenRead);
  ReturnValue := FIGoogleDriveApi.MultipartUpload(fs,'Test Multipart Image Upload from API.txt','test file','image/jpeg');
  FreeAndNil(fs);
  if(ReturnValue = nil) then
    fail('ReturnValue cannot be nil');
end;

任何想法可能导致问题?我什至不确定在这一点上应该怀疑什么。

4

2 回答 2

1

在我的代码中更改了两件事并进行了一些实质性的清理之后,我能够让上传工作。我所做的主要更改是将所有内容直接写入到TMemoryStream而不是aTStringStream然后写入aTMemoryStream并使用AnsiStrings 而不是字符串。这是我清理后的代码。

function TGoogleDriveApi.MultipartUpload(aStream: TStream; aFile: TGoogleDriveFileUp) : IGoogleDriveFile;
var
  json, url, boundary, body : AnsiString;
  lHttpHelper : IHttpHelper;
  ms : TMemoryStream;
  ss : TStringStream;
  writer : IRtSerializeWriter;
  reader : IRtSerializeReader;
begin
  if(Token.IsExpired) then
    Token.Refresh(TokenUrl, OAuth.ClientId, OAuth.ClientSecret);
  lHttpHelper := THttpHelper.Create;
  ss := TStringStream.Create;
  writer := TRtJsonSerializeWriter.Create;
  writer.SaveToStream(ss,aFile);
  ss.Position := 0;
  json := ss.ReadString(ss.Size);
  FreeAndNil(ss);
  url := 'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token='+lHttpHelper.UrlEncode(Token.access_token);
  boundary := 'a_bondary';
  body := '--'+boundary+sLineBreak;
  body := body + 'Content-Type: application/json; charset=UTF-8' + sLineBreak + sLineBreak;
  body := body + json + sLineBreak + sLineBreak +'--'+boundary;
  body := body + sLineBreak + sLineBreak;
  ms := TMemoryStream.Create;
  ms.Write(body[1],Length(body));
  ms.CopyFrom(aStream, aStream.Size);
  body := sLineBreak + sLineBreak + '--' + boundary + '--';
  ms.Write(body[1],Length(body));
  ms.Position := 0;
  json := lHttpHelper.PostResponse(url,'multipart/related; boundary="'+boundary+'"',ms);
  ss := TStringStream.Create;
  ss.WriteString(json);
  ss.Position := 0;
  reader := TRtJsonSerializeReader.Create;
  Result := TGoogleDriveFileDown.Create;
  reader.LoadFromStream(ss,Result as TGoogleDriveFileDown);
  FreeAndNil(ms);
  FreeAndNil(ss);
end;
于 2012-09-28T23:11:26.880 回答
1

您需要以某种方式遵循@RobKennedy 的建议。您需要查看线路上发生了什么来调试它。尝试用http替换https驱动器 url,然后使用 Wireshark 进行跟踪。

@Hendra 也是正确的,通常您的应用程序需要实现指数退避并重试 500 个错误,尽管我怀疑这不是您的具体问题(还 :-))

于 2012-09-28T07:48:49.613 回答