簡體   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