Unicode C
Unicode C
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 Unicode C Downloads
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
void ChilkatSample(void)
{
BOOL success;
HCkAuthGoogleW gAuth;
HCkRestW rest;
BOOL bAutoReconnect;
HCkJsonObjectW json;
int i;
int numFiles;
const wchar_t *jsonResponse;
int pageNumber;
const wchar_t *pageToken;
BOOL bContinueLoop;
BOOL bHasMorePages;
success = FALSE;
success = TRUE;
// 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.
gAuth = CkAuthGoogleW_Create();
CkAuthGoogleW_putAccessToken(gAuth,L"GOOGLE-DRIVE-ACCESS-TOKEN");
rest = CkRestW_Create();
// Connect using TLS.
bAutoReconnect = TRUE;
success = CkRestW_Connect(rest,L"www.googleapis.com",443,TRUE,bAutoReconnect);
// Provide the authentication credentials (i.e. the access token)
CkRestW_SetAuthGoogle(rest,gAuth);
// Get 5 results per page for testing. (The default page size is 100, with a max of 1000.
CkRestW_AddQueryParam(rest,L"pageSize",L"5");
json = CkJsonObjectW_Create();
// Send the request for the 1st page.
jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");
pageNumber = 1;
bContinueLoop = CkRestW_getLastMethodSuccess(rest) && (CkRestW_getResponseStatusCode(rest) == 200);
while (bContinueLoop == TRUE) {
wprintf(L"---- Page %d ----\n",pageNumber);
// Iterate over each file in the response and show the name, id, and mimeType.
CkJsonObjectW_Load(json,jsonResponse);
// See the sample JSON response at the bottom of this example.
numFiles = CkJsonObjectW_SizeOfArray(json,L"files");
i = 0;
while (i < numFiles) {
CkJsonObjectW_putI(json,i);
wprintf(L"name: %s\n",CkJsonObjectW_stringOf(json,L"files[i].name"));
wprintf(L"id: %s\n",CkJsonObjectW_stringOf(json,L"files[i].id"));
wprintf(L"mimeType: %s\n",CkJsonObjectW_stringOf(json,L"files[i].mimeType"));
wprintf(L"-\n");
i = i + 1;
}
// 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.
pageToken = CkJsonObjectW_stringOf(json,L"nextPageToken");
bContinueLoop = FALSE;
bHasMorePages = CkJsonObjectW_getLastMethodSuccess(json);
if (bHasMorePages == TRUE) {
CkRestW_ClearAllQueryParams(rest);
CkRestW_AddQueryParam(rest,L"pageSize",L"5");
CkRestW_AddQueryParam(rest,L"pageToken",pageToken);
jsonResponse = CkRestW_fullRequestNoBody(rest,L"GET",L"/drive/v3/files");
bContinueLoop = CkRestW_getLastMethodSuccess(rest) && (CkRestW_getResponseStatusCode(rest) == 200);
pageNumber = pageNumber + 1;
}
}
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
return;
}
// A successful response will have a status code equal to 200.
if (CkRestW_getResponseStatusCode(rest) != 200) {
wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest));
wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest));
wprintf(L"response header: %s\n",CkRestW_responseHeader(rest));
wprintf(L"response JSON: %s\n",jsonResponse);
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
return;
}
// 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"
// }
// ]
// }
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
}