Sample code for 30+ languages & platforms
Xbase++

List all Pages of Files in Google Drive

See more Google Drive Examples

Demonstrates how iterate over pages to list files in Google Drive.

See Google Drive Files list for more optional HTTP parameters.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oGAuth
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL oJson
LOCAL i
LOCAL nNumFiles
LOCAL cJsonResponse
LOCAL nPageNumber
LOCAL cPageToken
LOCAL nBContinueLoop
LOCAL nBHasMorePages

nSuccess := 0

nSuccess := 1

//  It requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  This example uses a previously obtained access token having permission for the 
//  Google Drive scope.

oGAuth := CreateObject("Chilkat.AuthGoogle")
oGAuth:AccessToken := "GOOGLE-DRIVE-ACCESS-TOKEN"

oRest := CreateObject("Chilkat.Rest")

//  Connect using TLS.
nBAutoReconnect := 1
nSuccess := oRest:Connect("www.googleapis.com", 443, 1, nBAutoReconnect)

//  Provide the authentication credentials (i.e. the access token)
oRest:SetAuthGoogle(oGAuth)

//  Get 5 results per page for testing.  (The default page size is 100, with a max of 1000.
oRest:AddQueryParam("pageSize", "5")

oJson := CreateObject("Chilkat.JsonObject")

//  Send the request for the 1st page.
cJsonResponse := oRest:FullRequestNoBody("GET", "/drive/v3/files")

nPageNumber := 1

nBContinueLoop := oRest:LastMethodSuccess .AND. (oRest:ResponseStatusCode == 200)

DO WHILE nBContinueLoop == 1

    ? "---- Page " + Str(nPageNumber) + " ----"

    //  Iterate over each file in the response and show the name, id, and mimeType.
    oJson:Load(cJsonResponse)

    //  See the sample JSON response at the bottom of this example.
    nNumFiles := oJson:SizeOfArray("files")
    i := 0
    DO WHILE i < nNumFiles
        oJson:I := i
        ? "name: " + oJson:StringOf("files[i].name")
        ? "id: " + oJson:StringOf("files[i].id")
        ? "mimeType: " + oJson:StringOf("files[i].mimeType")
        ? "-"
        i := i + 1
    ENDDO

    //  Get the next page of files.
    //  If the "nextPageToken" is present in the JSON response, then use it in the "pageToken" parameter
    //  for the next request.   If no "nextPageToken" was present, then this was the last page of files.
    cPageToken := oJson:StringOf("nextPageToken")
    nBContinueLoop := 0
    nBHasMorePages := oJson:LastMethodSuccess
    IF (nBHasMorePages == 1)
        oRest:ClearAllQueryParams()
        oRest:AddQueryParam("pageSize", "5")
        oRest:AddQueryParam("pageToken", cPageToken)
        cJsonResponse := oRest:FullRequestNoBody("GET", "/drive/v3/files")
        nBContinueLoop := oRest:LastMethodSuccess .AND. (oRest:ResponseStatusCode == 200)
        nPageNumber := nPageNumber + 1
    ENDIF

ENDDO

IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oGAuth:destroy()
    oRest:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  A successful response will have a status code equal to 200.
IF (oRest:ResponseStatusCode != 200)
    ? "response status code = " + Str(oRest:ResponseStatusCode)
    ? "response status text = " + oRest:ResponseStatusText
    ? "response header: " + oRest:ResponseHeader
    ? "response JSON: " + cJsonResponse
    oGAuth:destroy()
    oRest:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  A successful JSON response looks like this:
//  {
//   "kind": "drive#fileList",
//   "files": [
//     {
//       "kind": "drive#file",
//       "id": "0B53Q6OSTWYolenpjTEU4ekJlQUU",
//       "name": "test",
//       "mimeType": "application/vnd.google-apps.folder"
//     },
//     {
//       "kind": "drive#file",
//       "id": "0B53Q6OSTWYolRm4ycjZtdXhRaEE",
//       "name": "starfish4.jpg",
//       "mimeType": "image/jpeg"
//     },
//     {
//       "kind": "drive#file",
//       "id": "0B53Q6OSTWYolMWt2VzN0Qlo1UjA",
//       "name": "hamlet2.xml",
//       "mimeType": "text/xml"
//     },
//  ...
//     {
//       "kind": "drive#file",
//       "id": "0B53Q6OSTWYolc3RhcnRlcl9maWxlX2Rhc2hlclYw",
//       "name": "Getting started",
//       "mimeType": "application/pdf"
//     }
//   ]
//  }

oGAuth:destroy()
oRest:destroy()
oJson:destroy()