简体   繁体   中英

Delphi xe7 Rest to google drive api

I'm trying to upload file to google drive using OAuth2Authenticator1, RESTClient1, RESTRequest1, RESTResponse1. I've already get access token for scope : https://www.googleapis.com/auth/drive and it's ok. Now I'm trying to upload txt file, using next procedure:

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;  

I've allready manually create this folder on google drive : 'MyFolder'. HttpAnalyzer shows:

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

And google returns:

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"
 }
}

So i cant understand whats wrong with this procedure, can anyone help me?

Well, i modify it that way, and its working:

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;

But unfortunately, i cant add { "title":"MyFile"} construction to change uploaded file name. I've tried JSON object and TRESTRequestParameterKind.pkREQUESTBODY, but it's useless... Any ideas?

Also here's Folder creating procedure

   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;

But again it creates Folder with untitled name :(

For downloading i use this:

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 is document ID, which i get from parsed JSON List, but i have no idea which component to use to save Tfilestream on my hard drive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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