Unicode C
Unicode C
Delete All Files
See more Google Drive Examples
Permanently deletes all files owned by the user without moving it to the trash.This example works by first getting a list of all fileIds, and then iterating over the list to delete each file.
See Google Drive Files delete for more information.
Chilkat Unicode C Downloads
#include <C_CkAuthGoogleW.h>
#include <C_CkRestW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringArrayW.h>
#include <C_CkStringBuilderW.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;
const wchar_t *fileId;
HCkStringArrayW saFileIds;
BOOL bHasMorePages;
HCkStringBuilderW sbPath;
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 10 results per page for testing. (The default page size is 100, with a max of 1000.
CkRestW_AddQueryParam(rest,L"pageSize",L"10");
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);
saFileIds = CkStringArrayW_Create();
while (bContinueLoop == TRUE) {
wprintf(L"---- Page %d ----\n",pageNumber);
CkJsonObjectW_Load(json,jsonResponse);
numFiles = CkJsonObjectW_SizeOfArray(json,L"files");
i = 0;
while (i < numFiles) {
CkJsonObjectW_putI(json,i);
fileId = CkJsonObjectW_stringOf(json,L"files[i].id");
wprintf(L"name: %s\n",CkJsonObjectW_stringOf(json,L"files[i].name"));
wprintf(L"id: %s\n",fileId);
CkStringArrayW_Append(saFileIds,fileId);
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"10");
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;
}
}
// Before actually deleting, check for errors...
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkStringArrayW_Dispose(saFileIds);
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);
CkStringArrayW_Dispose(saFileIds);
return;
}
// OK, we have the full list of files. Delete each..
sbPath = CkStringBuilderW_Create();
numFiles = CkStringArrayW_getCount(saFileIds);
i = 0;
while (i < numFiles) {
fileId = CkStringArrayW_getString(saFileIds,i);
CkRestW_ClearAllQueryParams(rest);
CkStringBuilderW_Clear(sbPath);
CkStringBuilderW_Append(sbPath,L"/drive/v3/files/");
CkStringBuilderW_Append(sbPath,fileId);
jsonResponse = CkRestW_fullRequestNoBody(rest,L"DELETE",CkStringBuilderW_getAsString(sbPath));
if (CkRestW_getLastMethodSuccess(rest) != TRUE) {
wprintf(L"%s\n",CkRestW_lastErrorText(rest));
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkStringArrayW_Dispose(saFileIds);
CkStringBuilderW_Dispose(sbPath);
return;
}
// A successful response will have a status code equal to 204 and the response body is empty.
// (If not successful, then there should be a JSON response body with information..)
if (CkRestW_getResponseStatusCode(rest) != 204) {
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);
CkStringArrayW_Dispose(saFileIds);
CkStringBuilderW_Dispose(sbPath);
return;
}
i = i + 1;
wprintf(L"%d: Deleted %s\n",i,fileId);
}
wprintf(L"All Files Deleted.\n");
CkAuthGoogleW_Dispose(gAuth);
CkRestW_Dispose(rest);
CkJsonObjectW_Dispose(json);
CkStringArrayW_Dispose(saFileIds);
CkStringBuilderW_Dispose(sbPath);
}