繁体   English   中英

Delphi XE7 Rest转Google Drive API

[英]Delphi xe7 Rest to google drive api

我正在尝试使用OAuth2Authenticator1,RESTClient1,RESTRequest1,RESTResponse1将文件上传到Google驱动器。 我已经获取了作用域的访问令牌: https : //www.googleapis.com/auth/drive ,可以。 现在,我尝试使用下一个步骤上传txt文件:

procedure UploadtoDriveMy;
var
 LURL:String;
 cloud_path:String;
 upload_stream:TFileStream;
local_filename : string;
begin
 {$IF DEFINED(MsWindows)}
        local_filename :=  ExtractFilePath(ParamStr(0)) +'Test.txt';
 {$ENDIF}
 cloud_path:= 'MyFolder';
 LURL:= '/'+ HttpEncode(cloud_path)+'/';
 form2.RESTResponseDataSetAdapter1.AutoUpdate := false;
 form2.RESTRequest1.Method := form2.RESTRequestput.Method;
 form2.RESTClient1.BaseURL := 'https://www.googleapis.com/upload/drive/v2/files';
 form2.RESTRequest1.Resource := LURL;
 upload_stream := TFileStream.Create(local_filename,fmOpenRead);
 upload_stream.Position := 0;
form2.RESTRequest1.Params.AddHeader('Content-Type', 'text');
form2.RESTRequest1.ClearBody;
form2.RESTRequest1.AddBody(upload_stream,TRESTContentType.ctTEXT_PLAIN);
 try
  form2.RESTRequest1.Execute;
 except
on e: Exception do
begin
 ShowMessage(e.Message);
 end;
 end;
 upload_stream.Free;
end;  

我已经准备在Google驱动器上手动创建此文件夹:“ MyFolder”。 HttpAnalyzer显示:

PUT /upload/drive/v2/files/MyFolder/ HTTP/1.1
Content-Type: text/plain; charset=ISO-8859-1
Content-Length: 6
Content-Type: text
Authorization: Bearer     ya29.4AHLd5h0Rqc0oNPfOEUqYxCXRVMelg8x5C0AVlYPGU8fRzf3h_lDHB-KPT8cky_ZP8Pd
Host: www.googleapis.com
Accept: application/json, text/plain; q=0.9, text/html;q=0.8,
Accept-Charset: UTF-8, *;q=0.8
Accept-Encoding: identity
User-Agent: Embarcadero RESTClient/1.0

TestMY

谷歌返回:

HTTP/1.1 400 Bad Request
X-GUploader-UploadID: AEnB2UqMZW-wi1n28e_6klMgjju8F85VV-     BQ3TGiTPNR6j0_WX0muytirWKHL0_2TboT8tKYWcIHoQc6hrDz05s3T1WdZ-gS0w
Vary: Origin
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
Content-Length: 171
Date: Mon, 31 Aug 2015 11:19:24 GMT
Server: UploadServer
Alternate-Protocol: 443:quic,p=1
Alt-Svc: quic=":443"; p="1"; ma=604800

{
 "error": {
   "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

所以我无法理解此程序有什么问题,有人可以帮助我吗?

好吧,我以这种方式对其进行了修改,并且它的工作原理是:

procedure UploadtoDriveMyGolden2File;
var
 upload_stream:TFileStream;
 local_filename : string;
 ttt: TJSONObject;
begin
{$IF DEFINED(MsWindows)}
  local_filename :=  ExtractFilePath(ParamStr(0)) +'Northwindpers1.sqlite3';
{$ENDIF}
 form2.RESTResponseDataSetAdapter1.AutoUpdate := false;
 form2.RESTRequest1.Params.Clear;
 form2.RESTRequest1.ClearBody;
 form2.RESTRequest1.Method:=rmpost;
 form2.RESTClient1.BaseURL :=     'https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart';
  upload_stream := TFileStream.Create(local_filename,fmOpenRead);
  upload_stream.Position := 0;

  //Here I'm Trying to change title of uploaded file, but its doesnt work
  //ttt:= TJSONObject.create;
  //ttt.AddPair(TJSONPair.Create('Title', 'MyFile'));
  //form2.RESTRequest1.AddBody(ttt);
  //form2.RESTRequest1.AddParameter('Title','MyFile',TRESTRequestParameterKind.pkREQUESTBODY);

  form2.RESTRequest1.AddBody(upload_stream,     TRESTContentType.ctAPPLICATION_OCTET_STREAM);
try
  form2.RESTRequest1.Execute;
except
on e: Exception do
begin
  ShowMessage(e.Message);
   end;
   end;
  upload_stream.Free;
 //ttt.Free;
   end;

但不幸的是,我无法添加{“ title”:“ MyFile”}结构来更改上传的文件名。 我已经尝试过JSON对象和TRESTRequestParameterKind.pkREQUESTBODY,但是它没有用...有什么想法吗?

这也是文件夹创建过程

   procedure CreateFolder;
var
  parents: TJSONArray;
  Folder: TJSONObject;
begin
  form2.RESTResponseDataSetAdapter1.AutoUpdate := false;
  form2.RESTRequest1.Params.Clear;
  form2.RESTRequest1.ClearBody;
  form2.RESTRequest1.Method:=rmpost;
  form2.RESTClient1.BaseURL := 'https://www.googleapis.com/drive/v2/files';
 //Это вставка объекта
  Parents:= TJSONArray.Create;
  Folder:= TJSONObject.create;
      Folder.AddPair(TJSONPair.Create('Title', 'MyFolder1'));
      Folder.AddPair(TJSONPair.Create('mimeType', 'application/vnd.google-apps.folder'));
      Folder.AddPair(TJSONPair.Create('Parents', Parents));
  form2.RESTRequest1.AddBody(Folder);
try
  form2.RESTRequest1.Execute;
except
on e: Exception do
begin
  ShowMessage(e.Message);
end;
 end;
 Folder.Free;
 Parents.Free;
end;

但是,它再次使用无标题名称创建文件夹:(

为了下载我使用这个:

procedure DownloadfromDriveMyGolden2File;
begin

  form2.RESTResponseDataSetAdapter1.AutoUpdate := false;
  form2.RESTRequest1.Params.Clear;
  form2.RESTRequest1.ClearBody;
    form2.RESTRequest1.Method:=rmget;
  Form2.RESTClient1.BaseURL:='https://www.googleapis.com/drive/v2/files/{FileId}';
  form2.RESTRequest1.Resource := '';
  form2.RESTRequest1.Params.AddUrlSegment('FileId', form2.Edit4.Text);
try
  form2.RESTRequest1.Execute;

except
on e: Exception do
begin
  ShowMessage(e.Message);//Show Exception
end;
end;
end;

FileId是文档ID,我从解析的JSON列表中获取,但是我不知道该使用哪个组件将Tfilestream保存在硬盘上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM