Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

PureBasic Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(PureBasic) Google Cloud Storage List Buckets

Demonstrates how to retrieve a list of buckets for a given project.

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.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"

    ; In this example, Get Google Cloud Storage OAuth2 Access Token, 
    ; the service account access token was saved to a text file.  This example fetches the access token from the file..
    sbToken.i = CkStringBuilder::ckCreate()
    If sbToken.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckLoadFile(sbToken,"qa_data/tokens/googleCloudStorageAccessToken.txt","utf-8")

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttp::setCkAuthToken(http, CkStringBuilder::ckGetAsString(sbToken))

    ; For more info see Cloud Storage Documentation - Buckets: list 
    ; 
    success.i = CkHttp::ckSetUrlVar(http,"PROJECT_NAME","ChilkatCloud")
    resp.i = CkHttp::ckQuickGetObj(http,"https://www.googleapis.com/storage/v1/b?project={$PROJECT_NAME}")
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkStringBuilder::ckDispose(sbToken)
        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)

        CkStringBuilder::ckDispose(sbToken)
        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


    CkStringBuilder::ckDispose(sbToken)
    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.