Sample code for 30+ languages & platforms
Ruby

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

Ruby
require 'chilkat'

success = false

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

rest = Chilkat::CkRest.new()

# Connect to the www.dropbox.com endpoint.
bTls = true
port = 443
bAutoReconnect = true
success = rest.Connect("api.dropboxapi.com",port,bTls,bAutoReconnect)
if (success == false)
    print rest.lastErrorText() + "\n";
    exit
end

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

json = Chilkat::CkJsonObject.new()
# The root folder should be an empty string, not "/"
json.AppendString("path","")
json.AppendBool("recursive",false)
json.AppendBool("include_media_info",false)
json.AppendBool("include_deleted",false)
json.AppendBool("include_has_explicit_shared_members",false)

responseStr = rest.fullRequestString("POST","/2/files/list_folder",json.emit())
if (rest.get_LastMethodSuccess() == false)
    print rest.lastErrorText() + "\n";
    exit
end

# Success is indicated by a 200 response status code.
if (rest.get_ResponseStatusCode() != 200)
    # Examine the request/response to see what happened.
    print "response status code = " + rest.get_ResponseStatusCode().to_s() + "\n";
    print "response status text = " + rest.responseStatusText() + "\n";
    print "response header: " + rest.responseHeader() + "\n";
    print "response body (if any): " + responseStr + "\n";
    print "---" + "\n";
    print "LastRequestStartLine: " + rest.lastRequestStartLine() + "\n";
    print "LastRequestHeader: " + rest.lastRequestHeader() + "\n";
    exit
end

jsonResponse = Chilkat::CkJsonObject.new()
jsonResponse.Load(responseStr)

jsonResponse.put_EmitCompact(false)
print jsonResponse.emit() + "\n";

# A sample JSON response is shown at the end of this example.
# The following code iterates over the entries.
dt = Chilkat::CkDtObj.new()
numEntries = jsonResponse.SizeOfArray("entries")
i = 0
while i < numEntries
    jsonResponse.put_I(i)
    print "----" + "\n";
    print "name: " + jsonResponse.stringOf("entries[i].name") + "\n";
    print "path_lower: " + jsonResponse.stringOf("entries[i].path_lower") + "\n";
    print "path_display: " + jsonResponse.stringOf("entries[i].path_display") + "\n";
    if (jsonResponse.HasMember("entries[i].sharing_info") == true)
        print "has sharing_info..." + "\n";
        print "read_only: " + jsonResponse.stringOf("entries[i].sharing_info.read_only") + "\n";
        print "shared_folder_id: " + jsonResponse.stringOf("entries[i].sharing_info.shared_folder_id") + "\n";
    end

    if (jsonResponse.HasMember("entries[i].client_modified") == true)
        # Demonstrate how to parse a date/time:
        ckdt = Chilkat::CkDateTime.new()
        success = ckdt.SetFromTimestamp(jsonResponse.stringOf("entries[i].client_modified"))
        # The date/time can now be converted to many other formats, or the individual parts
        # can be accessed.
        bLocalDateTime = true
        ckdt.ToDtObj(bLocalDateTime,dt)

        print dt.get_Year().to_s() + "-" + dt.get_Month().to_s() + "-" + dt.get_Day().to_s() + "\n";
    end

    i = i + 1
end

print "Success." + "\n";

# {
#   "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
# }
#