Recent

Author Topic: GoogleAPI with Lazarus  (Read 4678 times)

e44

  • Newbie
  • Posts: 3
GoogleAPI with Lazarus
« on: August 20, 2021, 09:44:45 am »
Hello, I tried the drivedemo in lazarus, it works great to download a file from Googledrive, but I don't know how to upload and create a folder on Googledrive.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: GoogleAPI with Lazarus
« Reply #1 on: August 26, 2021, 03:14:10 pm »
The Google* units directly expose the corresponding API provided by Google. So you best take a look at Google's documentation of the Drive API.

e44

  • Newbie
  • Posts: 3
Re: GoogleAPI with Lazarus
« Reply #2 on: August 31, 2021, 11:19:53 pm »
Thank you PascalDragon
I made code to upload a text file to my Drive, the file is uploaded but the name is wrong "untitled"

my code
Code: Pascal  [Select][+][-]
  1. procedure TMainForm.Button4Click(Sender: TObject);
  2. Var
  3.   Request : TWebClientRequest;
  4.   Response: TWebClientResponse;
  5.   Bound,S,URL,LFN, FileName: ansiString;
  6.   Resource:TFilesResource  ;
  7.   List : TFileList;
  8. begin
  9.     LoadAuthConfig;
  10.     Resource:=Nil;
  11.     try
  12.        Resource:=FDriveAPI.CreateFilesResource(Self);
  13.        List:=Resource.list('root');
  14.        Request:=FClient.WebClient.CreateRequest;
  15.        FileName:= 'bbb.txt';
  16.        Bound := 'foo_bar_baz';
  17.        try
  18.            URL:= 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media';
  19.            Request.Headers.Add('Authorization: Bearer ' + FClient.AuthHandler.Session.AccessToken);
  20.            Request.Headers.Add('Content-Type: multipart/related; boundary="'+Bound+'"');
  21.            Request.Headers.Add('Accept: application/json');
  22.            s := CRLF + '--' + Bound + CRLF ;
  23.            s := s + 'Content-Type: application/json; charset=UTF-8' + CRLF  ;
  24.            s := s + '{' + CRLF;
  25.            s := s + '"name": "' + ExtractFileName(FileName)+ '"' + CRLF;
  26.            s := s + '}' + CRLF;
  27.            s := s + '--' + bound + CRLF ;
  28.            s := s + 'Content-Type: text/html' + CRLF + CRLF;
  29.            s := s + 'monTexte'+ CRLF;
  30.            s := s + '--' + bound + '--'+ CRLF ;
  31.            Request.SetContentFromString(S);
  32.            Request.Headers.Add('Content-Length: '+(S.Length).ToString);
  33.  
  34.            Response:=FClient.WebClient.ExecuteSignedRequest('POST',URL,Request);
  35.            try
  36.                 Memo1.Lines.LoadFromStream(Response.Content);  
  37.            finally     end;
  38.        finally
  39.             Request.Free;
  40.        end;
  41.     finally
  42.         FreeAndNil(Resource);
  43.     end;
  44. end;

the result is
Code: Pascal  [Select][+][-]
  1.  {
  2.  "kind": "drive#file",
  3.  "id": "1crNUrKj7buNMyZrfHOgZNtW4BOXWv-6I",
  4.  "name": "Untitled",
  5.  "mimeType": "text/html"
  6. }

and the request sended is

Code: Pascal  [Select][+][-]
  1. Authorization: Bearer ya30.a....r2TTGke
  2. Content-Type: multipart/related; boundary="foo_bar_baz"
  3. Accept: application/json
  4. Content-Length: 158
  5.  
  6.  
  7. --foo_bar_baz
  8. Content-Type: application/json; charset=UTF-8
  9. {
  10. "name": "bbb.txt"
  11. }
  12. --foo_bar_baz
  13. Content-Type: text/html
  14.  
  15. monTexte
  16. --foo_bar_baz--

is there someone see why the filename on drive is always "untitled" ?


« Last Edit: August 31, 2021, 11:22:36 pm by e44 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: GoogleAPI with Lazarus
« Reply #3 on: September 17, 2021, 07:45:49 am »
From the docs, seems like you've done it right. But my question is why you're doing the file creation with raw HTTP request? That Resource object contains Create method that can specify name and other properties.

e44

  • Newbie
  • Posts: 3
Re: GoogleAPI with Lazarus
« Reply #4 on: September 19, 2021, 04:50:58 pm »
thank you Leledumbo
yes the Resource object should create a path or a file, I tried but couldn't find a correct solution. And I didn't find any example on the internet.
I continue my search.

mgc

  • New Member
  • *
  • Posts: 12
Re: GoogleAPI with Lazarus
« Reply #5 on: May 17, 2022, 05:04:04 pm »
Hello, e44.
Have you found any solution?
I still looking for a way to upload a file using Drive API.
Thanks,
Maurício Camargo

 

TinyPortal © 2005-2018