Unicode C
Unicode C
Google Cloud Storage List Buckets
See more Google Cloud Storage Examples
Demonstrates how to retrieve a list of buckets for a given project.Chilkat Unicode C Downloads
#include <C_CkJsonObjectW.h>
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObjectW jsonToken;
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;
success = FALSE;
// 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 = CkHttpResponseW_Create();
success = CkHttpW_HttpNoBody(http,L"GET",L"https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}",resp);
if (success == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkJsonObjectW_Dispose(jsonToken);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
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");
CkJsonObjectW_Dispose(jsonToken);
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
return;
}
wprintf(L"Response code: %d\n",responseCode);
wprintf(L"Response body\n");
json = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(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);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(json);
}