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

Getting error while uploading files to google drive in Delphi

我正在尝试使用 Delphi 中的 RestAPI 将文件上传到 Google 驱动器。但无法将文件上传到云端硬盘。 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;

编辑:

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

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

有人可以建议如何给文件名/将同名文件上传到 google 驱动器。

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