【问题标题】:Getting error while uploading files to google drive in Delphi在 Delphi 中将文件上传到谷歌驱动器时出错
【发布时间】:2016-04-19 08:17:15
【问题描述】:

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

这是我的代码:

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'),现在文件已正确上传。

一切似乎都很好。但我注意到该文件正在以未命名的文件名上传,如下图所示。

有人可以建议如何提供文件名/将同名文件上传到谷歌驱动器。

【问题讨论】:

  • 你做过调试吗?您是否查看过您发送的请求的内容?
  • @DavidHeffernan,我调试了一下,发现漏了添加一行。我添加了,现在我看到了其他异常。我已经用同样的方法编辑了这个问题。你能告诉我是什么导致了这个错误吗?
  • 不过,你需要调试它。哪一行代码会产生这个错误?什么是堆栈跟踪?
  • try 中的行(在此处添加了注释“异常行”)导致错误。

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


【解决方案1】:

我终于能够将文件上传到谷歌驱动器。我更改了之前设置为LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/tasks') 的api url。我将其更改为LURL := LURL + '&scope=' + URIEncode('https://www.googleapis.com/auth/drive'),现在文件已正确上传。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多