Xbase++
Xbase++
List Files in Google Drive
See more Google Drive Examples
Demonstrates how to list files in Google Drive.See Google Drive Files list for more optional HTTP parameters.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oGAuth
LOCAL oRest
LOCAL nBAutoReconnect
LOCAL cJsonResponse
LOCAL oJson
LOCAL nNumFiles
LOCAL i
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 4 results per page. (The default page size is 100, with a max of 1000.
oRest:AddQueryParam("pageSize", "4")
// This uses the Google Drive V3 API... (not V2)
cJsonResponse := oRest:FullRequestNoBody("GET", "/drive/v3/files")
IF (oRest:LastMethodSuccess != 1)
? oRest:LastErrorText
oGAuth:destroy()
oRest:destroy()
RETURN
ENDIF
// A successful response will have a status code equal to 200.
IF (oRest:ResponseStatusCode != 200)
? "request header = " + oRest:LastRequestHeader
? "response status code = " + Str(oRest:ResponseStatusCode)
? "response status text = " + oRest:ResponseStatusText
? "response header: " + oRest:ResponseHeader
? "response JSON: " + cJsonResponse
oGAuth:destroy()
oRest:destroy()
RETURN
ENDIF
// A successful 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"
// }
// ]
// }
// Iterate over each file in the response and show the name, id, and mimeType.
oJson := CreateObject("Chilkat.JsonObject")
oJson:Load(cJsonResponse)
// Show the full JSON response.
oJson:EmitCompact := 0
? oJson:Emit()
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
oGAuth:destroy()
oRest:destroy()
oJson:destroy()