Delphi xe7休息到google drive api

时间:2015-08-31 14:13:05

标签: delphi oauth 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 drive上手动创建了这个文件夹:' 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

google返回:

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

所以我无法理解这个程序有什么问题,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

好吧,我这样修改它,它的工作原理:

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":#34; 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 List中获取,但我不知道使用哪个组件将Tfilestream保存在我的硬盘上。