Sample code for 30+ languages & platforms
PureBasic

Dropbox: Access Your Own Account

See more Dropbox Examples

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.

image

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ; 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 = CkRest::ckConnect(rest,"api.dropboxapi.com",port,bTls,bAutoReconnect)
    If success = 0
        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) = 0
        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.
    dt.i = CkDtObj::ckCreate()
    If dt.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    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
            CkDateTime::ckToDtObj(ckdt,bLocalDateTime,dt)

            Debug Str(CkDtObj::ckYear(dt)) + "-" + Str(CkDtObj::ckMonth(dt)) + "-" + Str(CkDtObj::ckDay(dt))
        EndIf

        i = i + 1
    Wend

    Debug "Success."

    ; {
    ;   "entries": [
    ;     {
    ;       ".tag": "folder",
    ;       "name": "chilkat Team Folder",
    ;       "path_lower": "/chilkat team folder",
    ;       "path_display": "/chilkat Team Folder",
    ;       "id": "id:2VqX9RLr-JAAAAAAAAAAAQ",
    ;       "shared_folder_id": "1210954290",
    ;       "sharing_info": {
    ;         "read_only": false,
    ;         "shared_folder_id": "1210954290"
    ;       }
    ;     },
    ;     {
    ;       ".tag": "file",
    ;       "name": "Get Started with Dropbox.pdf",
    ;       "path_lower": "/get started with dropbox.pdf",
    ;       "path_display": "/Get Started with Dropbox.pdf",
    ;       "id": "id:oxE2oMDqH4AAAAAAAAAAAQ",
    ;       "client_modified": "2016-05-07T11:47:47Z",
    ;       "server_modified": "2016-05-07T11:47:47Z",
    ;       "rev": "1483db13f",
    ;       "size": 692088
    ;     }
    ;   ],
    ;   "cursor": "AAEnZXciJyS4gzC_tlX6K2na4c8o7_09-dxmXKHpkPPyf3Kl0H4N-VYnz424nCrFOJuhMiM5_RgNAersumx9qe7NgCSdlppr80iFk0gf6vPlecM6SBtLcs6OYXL8ILWiZ62pfnOvgC3WKGlG6dqZ-VXH",
    ;   "has_more": false
    ; }
    ; 


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


    ProcedureReturn
EndProcedure