在Delphi中将文件上传到Google驱动器时出错

时间:2016-04-19 08:17:16

标签: delphi google-drive-api delphi-2010

我正在尝试使用Delphi中的RestAPI将文件上传到Google云端硬盘。但无法将文件上传到云端硬盘。 对Google驱动器的身份验证成功,但在上传文件时出错。 以下是我收到的错误消息。

enter image description here

这是我的代码:

var
  LURL: string;
  wv: Tfrm_OAuthWebForm;
  LToken: string;
   parents: TJSONArray;
  Folder: TJSONObject;
  upload_stream:TFileStream;
 local_filename : string;
 ttt: TJSONObject;
begin
  edt_GoogleTasks_AuthCode.Text := '';
  edt_GoogleTasks_AccessToken.Text := '';
  edt_GoogleTasks_RefreshToken.Text := '';

  /// step #1: get the auth-code
  LURL := 'https://accounts.google.com/o/oauth2/auth';
  LURL := LURL + '?response_type=' + URIEncode('code');
  LURL := LURL + '&client_id=' + URIEncode(edt_GoogleTasks_ClientID.Text);
  LURL := LURL + '&redirect_uri=' + URIEncode('urn:ietf:wg:oauth:2.0:oob');
  LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks');
  // optional
  // LURL := LURL + '&login_hint=' + URIEncode('user@example.com');

  wv := Tfrm_OAuthWebForm.Create(self);
  try
    wv.OnTitleChanged := self.OAuth2_GoogleTasks_BrowserTitleChanged;
    wv.ShowModalWithURL(LURL);
  finally
    wv.Release;
  end;

  /// step #2: get the access-token

  ResetRESTComponentsToDefaults;

  RESTClient.BaseURL := 'https://accounts.google.com/';

  RESTRequest.Method := TRESTRequestMethod.rmPOST;
  RESTRequest.Resource := 'o/oauth2/token';
  RESTRequest.Params.AddItem('code', edt_GoogleTasks_AuthCode.Text, TRESTRequestParameterKind.pkGETorPOST);
  RESTRequest.Params.AddItem('client_id', edt_GoogleTasks_ClientID.Text, TRESTRequestParameterKind.pkGETorPOST);
  RESTRequest.Params.AddItem('client_secret', edt_GoogleTasks_ClientSecret.Text, TRESTRequestParameterKind.pkGETorPOST);
  RESTRequest.Params.AddItem('redirect_uri', 'urn:ietf:wg:oauth:2.0:oob', TRESTRequestParameterKind.pkGETorPOST);
  RESTRequest.Params.AddItem('grant_type', 'authorization_code', TRESTRequestParameterKind.pkGETorPOST);

  RESTRequest.Execute;

  if RESTRequest.Response.GetSimpleValue('access_token', LToken) then
    // edt_GoogleTasks_AccessToken.Text := LToken;
    OAuth2_GoogleTasks.AccessToken := LToken;
  if RESTRequest.Response.GetSimpleValue('refresh_token', LToken) then
    // edt_GoogleTasks_RefreshToken.Text := LToken;
    OAuth2_GoogleTasks.RefreshToken := LToken;

    {$IF DEFINED(MsWindows)}
  local_filename :=  ExtractFilePath(ParamStr(0)) +'Sanjeev.txt';
{$ENDIF}
 RESTResponseDataSetAdapter.AutoUpdate := false;
 RESTRequest.Params.Clear;
                               RESTRequest.ClearBody;
 //RESTRequest.Method:=rmpost;
//try
 RESTClient.BaseURL :=     'https://www.googleapis.com/upload/drive/v2/files?';



      upload_stream := TFileStream.Create(local_filename,fmOpenReadWrite);
  upload_stream.Position := 0;
RESTRequest.AddBody(upload_stream,     TRESTContentType.ctAPPLICATION_OCTET_STREAM);

try
  RESTRequest.Execute;//Exception line
except
on e: Exception do
begin
  ShowMessage(e.Message);
   end;
       end;
  upload_stream.Free;

修改:

最后我能够将文件上传到谷歌硬盘。我更改了以前将其设置为LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks')的api url。我将其更改为LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/drive'),现在文件正在上传。

一切似乎都很好。但是我注意到文件上传时带有无标题文件名,如下图所示。

enter image description here

有人可以建议如何提供文件名/上传与google驱动器相同名称的文件。

1 个答案:

答案 0 :(得分:1)

最后我能够将文件上传到谷歌硬盘。我更改了以前将其设置为LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks')的api url。我将其更改为LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/drive'),现在文件正在上传。