Xbase++
Xbase++
Search for Files in Google Drive
See more Google Drive Examples
This example follows the same methodology for listing all files in Google Drive in pages, but applies a search filter. It shows how to apply a query parameter for filtering the file results. See the Google Drive Files list for more optional HTTP parameters.Chilkat Xbase++ Downloads
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")
// Our search filter is to list all files containing ".jpg" (i.e. all JPG image files)
oRest:AddQueryParam("q", "name contains '.jpg'")
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)
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)
oRest:AddQueryParam("q", "name contains '.jpg'")
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
oGAuth:destroy()
oRest:destroy()
oJson:destroy()