Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

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

local rest = chilkat.Rest.new()

-- Connect to the www.dropbox.com endpoint.
local bTls = true
local port = 443
local bAutoReconnect = true
local success = rest:connect("api.dropboxapi.com", port, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

rest:add_header("Content-Type", "application/json")
rest:add_header("Authorization", "Bearer DROPBOX-ACCESS-TOKEN")

local json = chilkat.JsonObject.new()
-- The root folder should be an empty string, not "/"
json:append_string("path", "")
json:append_bool("recursive", false)
json:append_bool("include_media_info", false)
json:append_bool("include_deleted", false)
json:append_bool("include_has_explicit_shared_members", false)

local responseStr = rest:full_request_string("POST", "/2/files/list_folder", json:emit())
if not rest.last_method_success then
    print(rest.last_error_text)
    return
end

-- Success is indicated by a 200 response status code.
if rest.response_status_code ~= 200 then
    -- Examine the request/response to see what happened.
    print("response status code = " .. rest.response_status_code)
    print("response status text = " .. rest.response_status_text)
    print("response header: " .. rest.response_header)
    print("response body (if any): " .. responseStr)
    print("---")
    print("LastRequestStartLine: " .. rest.last_request_start_line)
    print("LastRequestHeader: " .. rest.last_request_header)
    return
end

local jsonResponse = chilkat.JsonObject.new()
jsonResponse:load(responseStr)

jsonResponse.emit_compact = false
print(jsonResponse:emit())

-- A sample JSON response is shown at the end of this example.
-- The following code iterates over the entries.
local dt = chilkat.DtObj.new()
local numEntries = jsonResponse:size_of_array("entries")
local i = 0
while i < numEntries do
    jsonResponse.i = i
    print("----")
    print("name: " .. jsonResponse:string_of("entries[i].name"))
    print("path_lower: " .. jsonResponse:string_of("entries[i].path_lower"))
    print("path_display: " .. jsonResponse:string_of("entries[i].path_display"))
    if jsonResponse:has_member("entries[i].sharing_info") then
        print("has sharing_info...")
        print("read_only: " .. jsonResponse:string_of("entries[i].sharing_info.read_only"))
        print("shared_folder_id: " .. jsonResponse:string_of("entries[i].sharing_info.shared_folder_id"))
    end

    if jsonResponse:has_member("entries[i].client_modified") then
        -- Demonstrate how to parse a date/time:
        local ckdt = chilkat.DateTime.new()
        success = ckdt:set_from_timestamp(jsonResponse:string_of("entries[i].client_modified"))
        -- The date/time can now be converted to many other formats, or the individual parts
        -- can be accessed.
        local bLocalDateTime = true
        ckdt:to_dt_obj(bLocalDateTime, dt)

        print(dt.year .. "-" .. dt.month .. "-" .. dt.day)
    end

    i = i + 1
end

print("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
-- }
--