Sample code for 30+ languages & platforms
Rust

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

Rust

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

let rest = chilkat::Rest::new();

// Connect to the www.dropbox.com endpoint.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
if rest.connect("api.dropboxapi.com", port, b_tls, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let _ = rest.add_header("Content-Type", "application/json");
let _ = rest.add_header("Authorization", "Bearer DROPBOX-ACCESS-TOKEN");

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

let Ok(response_str) = rest.full_request_string("POST", "/2/files/list_folder", &json.emit().unwrap_or_default()) else {
    println!("{}", rest.last_error_text());
    return;
};

// Success is indicated by a 200 response status code.
if rest.response_status_code() != 200 {
    // Examine the request/response to see what happened.
    println!("response status code = {}", rest.response_status_code());
    println!("response status text = {}", rest.response_status_text());
    println!("response header: {}", rest.response_header());
    println!("response body (if any): {}", response_str);
    println!("---");
    println!("LastRequestStartLine: {}", rest.last_request_start_line());
    println!("LastRequestHeader: {}", rest.last_request_header());
    return;
}

let json_response = chilkat::JsonObject::new();
let _ = json_response.load(&response_str);

json_response.set_emit_compact(false);
println!("{}", json_response.emit().unwrap_or_default());

// A sample JSON response is shown at the end of this example.
// The following code iterates over the entries.
let dt = chilkat::DtObj::new();
let num_entries = json_response.size_of_array("entries");
let mut i = 0;
while i < num_entries {
    json_response.set_i(i);
    println!("----");
    println!("name: {}", json_response.string_of("entries[i].name").unwrap_or_default());
    println!("path_lower: {}", json_response.string_of("entries[i].path_lower").unwrap_or_default());
    println!("path_display: {}", json_response.string_of("entries[i].path_display").unwrap_or_default());
    if json_response.has_member("entries[i].sharing_info") {
        println!("has sharing_info...");
        println!("read_only: {}", json_response.string_of("entries[i].sharing_info.read_only").unwrap_or_default());
        println!("shared_folder_id: {}", json_response.string_of("entries[i].sharing_info.shared_folder_id").unwrap_or_default());
    }

    if json_response.has_member("entries[i].client_modified") {
        // Demonstrate how to parse a date/time:
        let ckdt = chilkat::DateTime::new();
        let _ = ckdt.set_from_timestamp(&json_response.string_of("entries[i].client_modified").unwrap_or_default()).is_ok();
        // The date/time can now be converted to many other formats, or the individual parts
        // can be accessed.
        let b_local_date_time = true;
        ckdt.to_dt_obj(b_local_date_time, &dt);

        println!("{}-{}-{}", dt.year(), dt.month(), dt.day());
    }

    i = i + 1;
}

println!("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
// }
//