Forum > Networking and Web Programming
GoogleAPI with Lazarus
e44:
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:
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMainForm.Button4Click(Sender: TObject);Var Request : TWebClientRequest; Response: TWebClientResponse; Bound,S,URL,LFN, FileName: ansiString; Resource:TFilesResource ; List : TFileList;begin LoadAuthConfig; Resource:=Nil; try Resource:=FDriveAPI.CreateFilesResource(Self); List:=Resource.list('root'); Request:=FClient.WebClient.CreateRequest; FileName:= 'bbb.txt'; Bound := 'foo_bar_baz'; try URL:= 'https://www.googleapis.com/upload/drive/v3/files?uploadType=media'; Request.Headers.Add('Authorization: Bearer ' + FClient.AuthHandler.Session.AccessToken); Request.Headers.Add('Content-Type: multipart/related; boundary="'+Bound+'"'); Request.Headers.Add('Accept: application/json'); s := CRLF + '--' + Bound + CRLF ; s := s + 'Content-Type: application/json; charset=UTF-8' + CRLF ; s := s + '{' + CRLF; s := s + '"name": "' + ExtractFileName(FileName)+ '"' + CRLF; s := s + '}' + CRLF; s := s + '--' + bound + CRLF ; s := s + 'Content-Type: text/html' + CRLF + CRLF; s := s + 'monTexte'+ CRLF; s := s + '--' + bound + '--'+ CRLF ; Request.SetContentFromString(S); Request.Headers.Add('Content-Length: '+(S.Length).ToString); Response:=FClient.WebClient.ExecuteSignedRequest('POST',URL,Request); try Memo1.Lines.LoadFromStream(Response.Content); finally end; finally Request.Free; end; finally FreeAndNil(Resource); end;end;
the result is
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- { "kind": "drive#file", "id": "1crNUrKj7buNMyZrfHOgZNtW4BOXWv-6I", "name": "Untitled", "mimeType": "text/html"}
and the request sended is
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Authorization: Bearer ya30.a....r2TTGkeContent-Type: multipart/related; boundary="foo_bar_baz"Accept: application/jsonContent-Length: 158 --foo_bar_bazContent-Type: application/json; charset=UTF-8{"name": "bbb.txt"}--foo_bar_bazContent-Type: text/html monTexte--foo_bar_baz--
is there someone see why the filename on drive is always "untitled" ?
Leledumbo:
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:
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.
Navigation
[0] Message Index
[#] Next page