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) Google Cloud Storage List BucketsSee more Google Cloud Storage ExamplesDemonstrates how to retrieve a list of buckets for a given project.
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkJsonObject.pb" Procedure ChilkatExample() ; This example 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 ; scope "https://www.googleapis.com/auth/cloud-platform" jsonToken.i = CkJsonObject::ckCreate() If jsonToken.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i = CkJsonObject::ckLoadFile(jsonToken,"qa_data/tokens/googleCloudStorage.json") If success = 0 Debug CkJsonObject::ckLastErrorText(jsonToken) CkJsonObject::ckDispose(jsonToken) ProcedureReturn EndIf http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttp::setCkAuthToken(http, CkJsonObject::ckStringOf(jsonToken,"access_token")) ; For more info see Cloud Storage Documentation - Buckets: list ; success = CkHttp::ckSetUrlVar(http,"PROJECT_ID","chilkattest-1050") resp.i = CkHttp::ckQuickGetObj(http,"https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}") If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkJsonObject::ckDispose(jsonToken) CkHttp::ckDispose(http) ProcedureReturn EndIf responseCode.i = CkHttpResponse::ckStatusCode(resp) If responseCode = 401 Debug CkHttpResponse::ckBodyStr(resp) Debug "If invalid credentials, then it is likely the access token expired." Debug "Your app should automatically fetch a new access token and re-try." CkHttpResponse::ckDispose(resp) CkJsonObject::ckDispose(jsonToken) CkHttp::ckDispose(http) ProcedureReturn EndIf Debug "Response code: " + Str(responseCode) Debug "Response body" json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp)) CkHttpResponse::ckDispose(resp) CkJsonObject::setCkEmitCompact(json, 0) Debug CkJsonObject::ckEmit(json) ; A response code = 200 indicates success, and the response body contains JSON such as this: ; { ; "kind": "storage#buckets", ; "items": [ ; { ; "kind": "storage#bucket", ; "id": "chilkat-bucket", ; "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket", ; "projectNumber": "5332332985", ; "name": "chilkat-bucket", ; "timeCreated": "2018-10-23T00:04:44.507Z", ; "updated": "2018-10-23T00:04:44.507Z", ; "metageneration": "1", ; "iamConfiguration": { ; "bucketPolicyOnly": { ; "enabled": false ; } ; }, ; "location": "US", ; "storageClass": "MULTI_REGIONAL", ; "etag": "CAE=" ; }, ; { ; "kind": "storage#bucket", ; "id": "chilkat-images", ; "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images", ; "projectNumber": "5332332985", ; "name": "chilkat-images", ; "timeCreated": "2018-10-23T11:24:43.000Z", ; "updated": "2018-10-23T11:24:43.000Z", ; "metageneration": "1", ; "iamConfiguration": { ; "bucketPolicyOnly": { ; "enabled": false ; } ; }, ; "location": "US", ; "storageClass": "MULTI_REGIONAL", ; "etag": "CAE=" ; } ; ] ; } ; Use this online tool to generate parsing code from sample JSON: ; Generate Parsing Code from JSON kind.s i.i count_i.i id.s selfLink.s projectNumber.s name.s timeCreated.s updated.s metageneration.s iamConfigurationBucketPolicyOnlyEnabled.i location.s storageClass.s etag.s kind = CkJsonObject::ckStringOf(json,"kind") i = 0 count_i = CkJsonObject::ckSizeOfArray(json,"items") While i < count_i CkJsonObject::setCkI(json, i) kind = CkJsonObject::ckStringOf(json,"items[i].kind") id = CkJsonObject::ckStringOf(json,"items[i].id") selfLink = CkJsonObject::ckStringOf(json,"items[i].selfLink") projectNumber = CkJsonObject::ckStringOf(json,"items[i].projectNumber") name = CkJsonObject::ckStringOf(json,"items[i].name") timeCreated = CkJsonObject::ckStringOf(json,"items[i].timeCreated") updated = CkJsonObject::ckStringOf(json,"items[i].updated") metageneration = CkJsonObject::ckStringOf(json,"items[i].metageneration") iamConfigurationBucketPolicyOnlyEnabled = CkJsonObject::ckBoolOf(json,"items[i].iamConfiguration.bucketPolicyOnly.enabled") location = CkJsonObject::ckStringOf(json,"items[i].location") storageClass = CkJsonObject::ckStringOf(json,"items[i].storageClass") etag = CkJsonObject::ckStringOf(json,"items[i].etag") i = i + 1 Wend CkJsonObject::ckDispose(jsonToken) CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.