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) Dropbox: Access Your Own Account

To programmatically interact with your own Dropbox account, such as for uploading, downloading, listing files, etc., go to the Dropbox app console and create an application. Then generate an access token in the online app console as shown in the screenshot below. The access token does not expire, and can be used to access your own account from your own non-web application.

The example code, located below the screenshot, shows how to list the files in the root folder.

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkDtObj.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkDateTime.pb"

Procedure ChilkatExample()

    ;  This example requires the Chilkat API to have been previously unlocked.
    ;  See Global Unlock Sample for sample code.

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

    ;  Connect to the www.dropbox.com endpoint.
    bTls.i = 1
    port.i = 443
    bAutoReconnect.i = 1
    success.i = CkRest::ckConnect(rest,"api.dropboxapi.com",port,bTls,bAutoReconnect)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    CkRest::ckAddHeader(rest,"Content-Type","application/json")
    CkRest::ckAddHeader(rest,"Authorization","Bearer DROPBOX_ACCESS_TOKEN")

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

    ;  The root folder should be an empty string, not "/"
    CkJsonObject::ckAppendString(json,"path","")
    CkJsonObject::ckAppendBool(json,"recursive",0)
    CkJsonObject::ckAppendBool(json,"include_media_info",0)
    CkJsonObject::ckAppendBool(json,"include_deleted",0)
    CkJsonObject::ckAppendBool(json,"include_has_explicit_shared_members",0)

    responseStr.s = CkRest::ckFullRequestString(rest,"POST","/2/files/list_folder",CkJsonObject::ckEmit(json))
    If CkRest::ckLastMethodSuccess(rest) <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ;  Success is indicated by a 200 response status code.
    If CkRest::ckResponseStatusCode(rest) <> 200
        ;  Examine the request/response to see what happened.
        Debug "response status code = " + Str(CkRest::ckResponseStatusCode(rest))
        Debug "response status text = " + CkRest::ckResponseStatusText(rest)
        Debug "response header: " + CkRest::ckResponseHeader(rest)
        Debug "response body (if any): " + responseStr
        Debug "---"
        Debug "LastRequestStartLine: " + CkRest::ckLastRequestStartLine(rest)
        Debug "LastRequestHeader: " + CkRest::ckLastRequestHeader(rest)
        CkRest::ckDispose(rest)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoad(jsonResponse,responseStr)

    CkJsonObject::setCkEmitCompact(jsonResponse, 0)
    Debug CkJsonObject::ckEmit(jsonResponse)

    ;  A sample JSON response is shown at the end of this example.
    ;  The following code iterates over the entries.
    numEntries.i = CkJsonObject::ckSizeOfArray(jsonResponse,"entries")
    i.i = 0
    While i < numEntries
        CkJsonObject::setCkI(jsonResponse, i)
        Debug "----"
        Debug "name: " + CkJsonObject::ckStringOf(jsonResponse,"entries[i].name")
        Debug "path_lower: " + CkJsonObject::ckStringOf(jsonResponse,"entries[i].path_lower")
        Debug "path_display: " + CkJsonObject::ckStringOf(jsonResponse,"entries[i].path_display")
        If CkJsonObject::ckHasMember(jsonResponse,"entries[i].sharing_info") = 1
            Debug "has sharing_info..."
            Debug "read_only: " + CkJsonObject::ckStringOf(jsonResponse,"entries[i].sharing_info.read_only")
            Debug "shared_folder_id: " + CkJsonObject::ckStringOf(jsonResponse,"entries[i].sharing_info.shared_folder_id")
        EndIf

        If CkJsonObject::ckHasMember(jsonResponse,"entries[i].client_modified") = 1
            ;  Demonstrate how to parse a date/time:
            ckdt.i = CkDateTime::ckCreate()
            If ckdt.i = 0
                Debug "Failed to create object."
                ProcedureReturn
            EndIf

            success = CkDateTime::ckSetFromTimestamp(ckdt,CkJsonObject::ckStringOf(jsonResponse,"entries[i].client_modified"))
            ;  The date/time can now be converted to many other formats, or the individual parts
            ;  can be accessed.
            bLocalDateTime.i = 1
            dt.i = CkDateTime::ckGetDtObj(ckdt,bLocalDateTime)
            Debug Str(CkDtObj::ckYear(dt)) + "-" + Str(CkDtObj::ckMonth(dt)) + "-" + Str(CkDtObj::ckDay(dt))
            CkDtObj::ckDispose(dt)

        EndIf

        i = i + 1
    Wend

    Debug "Success."

    ; --1--


    CkRest::ckDispose(rest)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonResponse)
    CkDateTime::ckDispose(ckdt)


    ProcedureReturn
EndProcedure

 

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