Delphi DLL Google Drive: List Files

Back to Index

Lists files..

Documentation: https://developers.google.com/drive/v3/reference/files/list


var
rest: HCkRest;
success: Boolean;
oauth2: HCkOAuth2;
sbJson: HCkStringBuilder;
json: HCkJsonObject;
kind: PWideChar;
incompleteSearch: Boolean;
i: Integer;
count_i: Integer;
id: PWideChar;
name: PWideChar;
mimeType: PWideChar;

begin
rest := CkRest_Create();

//   Provide a previously obtained OAuth2 access token.
oauth2 := CkOAuth2_Create();
CkOAuth2_putAccessToken(oauth2,'OAUTH2_ACCESS_TOKEN');
CkRest_SetAuthOAuth2(rest,oauth2);

success := CkRest_Connect(rest,'www.googleapis.com',443,True,True);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

sbJson := CkStringBuilder_Create();
success := CkRest_FullRequestNoBodySb(rest,'GET','/drive/v3/files',sbJson);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Received error response code: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
    Memo1.Lines.Add('Response body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbJson));
    Exit;
  end;

json := CkJsonObject_Create();
CkJsonObject_LoadSb(json,sbJson);

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.

kind := CkJsonObject__stringOf(json,'kind');
incompleteSearch := CkJsonObject_BoolOf(json,'incompleteSearch');
i := 0;
count_i := CkJsonObject_SizeOfArray(json,'files');
while i < count_i do
  begin
CkJsonObject_putI(json,i);
    kind := CkJsonObject__stringOf(json,'files[i].kind');
    id := CkJsonObject__stringOf(json,'files[i].id');
    name := CkJsonObject__stringOf(json,'files[i].name');
    mimeType := CkJsonObject__stringOf(json,'files[i].mimeType');
    i := i + 1;
  end;

Memo1.Lines.Add('Example Completed.');

CkRest_Dispose(rest);
CkOAuth2_Dispose(oauth2);
CkStringBuilder_Dispose(sbJson);
CkJsonObject_Dispose(json);

Sample JSON Response Body

{
 "kind": "drive#fileList",
 "incompleteSearch": false,
 "files": [
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolT1E4blNEaHlBVTg",
   "name": "misc",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B84ttUI7E_NyWHBYNUlFRGZSWVE",
   "name": "02112017091435553-LW0826-058444946-4C414E43.xml",
   "mimeType": "application/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B5w0YPzxv8g6SmRDTWUweEJCeVk",
   "name": "Sales in 2004.pdf",
   "mimeType": "application/pdf"
  },
  {
   "kind": "drive#file",
   "id": "0By-hrxCCDzSQUGxJY0hzNUFpNmc",
   "name": "helloWorld.txt",
   "mimeType": "text/plain"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolMjZhSkV0a2tUX3c",
   "name": "hamletC.xml",
   "mimeType": "text/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolVlA1d0tscUtRLXc",
   "name": "chilkat",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolelZ2UGdPZmlJTmc",
   "name": "hamlet2.xml",
   "mimeType": "text/xml"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolaWNqLWJqLUhwb2M",
   "name": "starfish3.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolNlFnbDhWZ3g3UGc",
   "name": "starfish4.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolQlExSlBQT1phZXM",
   "name": "pigs.json",
   "mimeType": "attachment/json"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolVHRPVkxtYWFtZkk",
   "name": "starfish20.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolRGZEV3ZGUTZfNFk",
   "name": "penguins2.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolS2FXSjliMXQxSU0",
   "name": "starfish.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolREhVUzVWRUlFeGM",
   "name": "Пингвины.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolZUhxckMzb0dRMzg",
   "name": "starfish.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolbUF6WS1Gei1oalk",
   "name": "penguins.jpg",
   "mimeType": "image/jpeg"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYola296ODZUSm5GYU0",
   "name": "123",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolbTE3c3J5RHBUcHM",
   "name": "abc",
   "mimeType": "application/vnd.google-apps.folder"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolTmhybWJSUGd5Q2c",
   "name": "testHello.txt",
   "mimeType": "text/plain"
  },
  {
   "kind": "drive#file",
   "id": "0B53Q6OSTWYolY2tPU1BnYW02T2c",
   "name": "testFolder",
   "mimeType": "application/vnd.google-apps.folder"
  }
 ]
}