Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Delete All FilesPermanently 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.
IncludeFile "CkStringBuilder.pb" IncludeFile "CkAuthGoogle.pb" IncludeFile "CkRest.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkStringArray.pb" Procedure ChilkatExample() success.i = 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. gAuth.i = CkAuthGoogle::ckCreate() If gAuth.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkAuthGoogle::setCkAccessToken(gAuth, "GOOGLE-DRIVE-ACCESS-TOKEN") rest.i = CkRest::ckCreate() If rest.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; Connect using TLS. bAutoReconnect.i = 1 success = CkRest::ckConnect(rest,"www.googleapis.com",443,1,bAutoReconnect) ; Provide the authentication credentials (i.e. the access token) CkRest::ckSetAuthGoogle(rest,gAuth) ; Get 10 results per page for testing. (The default page size is 100, with a max of 1000. CkRest::ckAddQueryParam(rest,"pageSize","10") json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf i.i numFiles.i ; Send the request for the 1st page. jsonResponse.s = CkRest::ckFullRequestNoBody(rest,"GET","/drive/v3/files") pageNumber.i = 1 pageToken.s bContinueLoop.i = CkRest::ckLastMethodSuccess(rest) AND (CkRest::ckResponseStatusCode(rest) = 200) fileId.s saFileIds.i = CkStringArray::ckCreate() If saFileIds.i = 0 Debug "Failed to create object." ProcedureReturn EndIf While bContinueLoop = 1 Debug "---- Page " + Str(pageNumber) + " ----" CkJsonObject::ckLoad(json,jsonResponse) numFiles = CkJsonObject::ckSizeOfArray(json,"files") i = 0 While i < numFiles CkJsonObject::setCkI(json, i) fileId = CkJsonObject::ckStringOf(json,"files[i].id") Debug "name: " + CkJsonObject::ckStringOf(json,"files[i].name") Debug "id: " + fileId CkStringArray::ckAppend(saFileIds,fileId) i = i + 1 Wend ; 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 = CkJsonObject::ckStringOf(json,"nextPageToken") bContinueLoop = 0 bHasMorePages.i = CkJsonObject::ckLastMethodSuccess(json) If bHasMorePages = 1 CkRest::ckClearAllQueryParams(rest) CkRest::ckAddQueryParam(rest,"pageSize","10") CkRest::ckAddQueryParam(rest,"pageToken",pageToken) jsonResponse = CkRest::ckFullRequestNoBody(rest,"GET","/drive/v3/files") bContinueLoop = CkRest::ckLastMethodSuccess(rest) AND (CkRest::ckResponseStatusCode(rest) = 200) pageNumber = pageNumber + 1 EndIf Wend ; Before actually deleting, check for errors... If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkAuthGoogle::ckDispose(gAuth) CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) CkStringArray::ckDispose(saFileIds) ProcedureReturn EndIf ; A successful response will have a status code equal to 200. If CkRest::ckResponseStatusCode(rest) <> 200 Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest)) Debug "response status text = " + CkRest::ckResponseStatusText(rest) Debug "response header: " + CkRest::ckResponseHeader(rest) Debug "response JSON: " + jsonResponse CkAuthGoogle::ckDispose(gAuth) CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) CkStringArray::ckDispose(saFileIds) ProcedureReturn EndIf ; OK, we have the full list of files. Delete each.. sbPath.i = CkStringBuilder::ckCreate() If sbPath.i = 0 Debug "Failed to create object." ProcedureReturn EndIf numFiles = CkStringArray::ckCount(saFileIds) i = 0 While i < numFiles fileId = CkStringArray::ckGetString(saFileIds,i) CkRest::ckClearAllQueryParams(rest) CkStringBuilder::ckClear(sbPath) CkStringBuilder::ckAppend(sbPath,"/drive/v3/files/") CkStringBuilder::ckAppend(sbPath,fileId) jsonResponse = CkRest::ckFullRequestNoBody(rest,"DELETE",CkStringBuilder::ckGetAsString(sbPath)) If CkRest::ckLastMethodSuccess(rest) <> 1 Debug CkRest::ckLastErrorText(rest) CkAuthGoogle::ckDispose(gAuth) CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) CkStringArray::ckDispose(saFileIds) CkStringBuilder::ckDispose(sbPath) ProcedureReturn EndIf ; 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 CkRest::ckResponseStatusCode(rest) <> 204 Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest)) Debug "response status text = " + CkRest::ckResponseStatusText(rest) Debug "response header: " + CkRest::ckResponseHeader(rest) Debug "response JSON: " + jsonResponse CkAuthGoogle::ckDispose(gAuth) CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) CkStringArray::ckDispose(saFileIds) CkStringBuilder::ckDispose(sbPath) ProcedureReturn EndIf i = i + 1 Debug Str(i) + ": Deleted " + fileId Wend Debug "All Files Deleted." CkAuthGoogle::ckDispose(gAuth) CkRest::ckDispose(rest) CkJsonObject::ckDispose(json) CkStringArray::ckDispose(saFileIds) CkStringBuilder::ckDispose(sbPath) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.