Sample code for 30+ languages & platforms
Dart

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

Dart
import 'package:chilkat/chilkat.dart';

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

  final rest = CkRest();

  // Connect to the www.dropbox.com endpoint.
  final bTls = true;
  final port = 443;
  final bAutoReconnect = true;
  try {
    rest.connect('api.dropboxapi.com', port, bTls, bAutoReconnect);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  rest.addHeader('Content-Type', 'application/json');
  rest.addHeader('Authorization', 'Bearer DROPBOX-ACCESS-TOKEN');

  final json = CkJsonObject();
  // 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);

  final String responseStr;
  try {
    responseStr = rest.fullRequestString('POST', '/2/files/list_folder', json.emit());
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Success is indicated by a 200 response status code.
  if (rest.responseStatusCode != 200) {
    // Examine the request/response to see what happened.
    print('response status code = ${rest.responseStatusCode}');
    print('response status text = ${rest.responseStatusText}');
    print('response header: ${rest.responseHeader}');
    print('response body (if any): $responseStr');
    print('---');
    print('LastRequestStartLine: ${rest.lastRequestStartLine}');
    print('LastRequestHeader: ${rest.lastRequestHeader}');
    return;
  }

  final jsonResponse = CkJsonObject();
  jsonResponse.load(responseStr);

  jsonResponse.emitCompact = false;
  print(jsonResponse.emit());

  // A sample JSON response is shown at the end of this example.
  // The following code iterates over the entries.
  final dt = CkDtObj();
  final numEntries = jsonResponse.sizeOfArray('entries');
  var i = 0;
  while (i < numEntries) {
    jsonResponse.i = i;
    print('----');
    print('name: ${jsonResponse.stringOf('entries[i].name')}');
    print('path_lower: ${jsonResponse.stringOf('entries[i].path_lower')}');
    print('path_display: ${jsonResponse.stringOf('entries[i].path_display')}');
    if (jsonResponse.hasMember('entries[i].sharing_info')) {
      print('has sharing_info...');
      print('read_only: ${jsonResponse.stringOf('entries[i].sharing_info.read_only')}');
      print('shared_folder_id: ${jsonResponse.stringOf('entries[i].sharing_info.shared_folder_id')}');
    }

    if (jsonResponse.hasMember('entries[i].client_modified')) {
      // Demonstrate how to parse a date/time:
      final ckdt = CkDateTime();
      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.
      final bLocalDateTime = true;
      ckdt.toDtObj(bLocalDateTime, dt);

      print('${dt.year}-${dt.month}-${dt.day}');
    }

    i++;
  }

  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
  // }
  // 
}