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
(Unicode C) Google Cloud Storage List BucketsSee more Google Cloud Storage ExamplesDemonstrates how to retrieve a list of buckets for a given project.
#include <C_CkJsonObjectW.h> #include <C_CkHttpW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkJsonObjectW jsonToken; BOOL success; HCkHttpW http; HCkHttpResponseW resp; int responseCode; HCkJsonObjectW json; const wchar_t *kind; int i; int count_i; const wchar_t *id; const wchar_t *selfLink; const wchar_t *projectNumber; const wchar_t *name; const wchar_t *timeCreated; const wchar_t *updated; const wchar_t *metageneration; BOOL iamConfigurationBucketPolicyOnlyEnabled; const wchar_t *location; const wchar_t *storageClass; const wchar_t *etag; // 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 = CkJsonObjectW_Create(); success = CkJsonObjectW_LoadFile(jsonToken,L"qa_data/tokens/googleCloudStorage.json"); if (success == FALSE) { wprintf(L"%s\n",CkJsonObjectW_lastErrorText(jsonToken)); CkJsonObjectW_Dispose(jsonToken); return; } http = CkHttpW_Create(); CkHttpW_putAuthToken(http,CkJsonObjectW_stringOf(jsonToken,L"access_token")); // For more info see Cloud Storage Documentation - Buckets: list // success = CkHttpW_SetUrlVar(http,L"PROJECT_ID",L"chilkattest-1050"); resp = CkHttpW_QuickGetObj(http,L"https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}"); if (CkHttpW_getLastMethodSuccess(http) == FALSE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); return; } responseCode = CkHttpResponseW_getStatusCode(resp); if (responseCode == 401) { wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); wprintf(L"If invalid credentials, then it is likely the access token expired.\n"); wprintf(L"Your app should automatically fetch a new access token and re-try.\n"); CkHttpResponseW_Dispose(resp); CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); return; } wprintf(L"Response code: %d\n",responseCode); wprintf(L"Response body\n"); json = CkJsonObjectW_Create(); success = CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkHttpResponseW_Dispose(resp); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(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 = CkJsonObjectW_stringOf(json,L"kind"); i = 0; count_i = CkJsonObjectW_SizeOfArray(json,L"items"); while (i < count_i) { CkJsonObjectW_putI(json,i); kind = CkJsonObjectW_stringOf(json,L"items[i].kind"); id = CkJsonObjectW_stringOf(json,L"items[i].id"); selfLink = CkJsonObjectW_stringOf(json,L"items[i].selfLink"); projectNumber = CkJsonObjectW_stringOf(json,L"items[i].projectNumber"); name = CkJsonObjectW_stringOf(json,L"items[i].name"); timeCreated = CkJsonObjectW_stringOf(json,L"items[i].timeCreated"); updated = CkJsonObjectW_stringOf(json,L"items[i].updated"); metageneration = CkJsonObjectW_stringOf(json,L"items[i].metageneration"); iamConfigurationBucketPolicyOnlyEnabled = CkJsonObjectW_BoolOf(json,L"items[i].iamConfiguration.bucketPolicyOnly.enabled"); location = CkJsonObjectW_stringOf(json,L"items[i].location"); storageClass = CkJsonObjectW_stringOf(json,L"items[i].storageClass"); etag = CkJsonObjectW_stringOf(json,L"items[i].etag"); i = i + 1; } CkJsonObjectW_Dispose(jsonToken); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.