React Native
React Native
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.
Chilkat React Native Downloads
import { CkDateTime, DtObj, JsonObject, Rest } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = new Rest();
// Connect to the www.dropbox.com endpoint.
const bTls = true;
const port = 443;
const bAutoReconnect = true;
try {
await rest.connectAsync('api.dropboxapi.com', port, bTls, bAutoReconnect);
} catch {
console.log(rest.lastErrorText);
return;
}
rest.addHeader('Content-Type', 'application/json');
rest.addHeader('Authorization', 'Bearer DROPBOX-ACCESS-TOKEN');
const json = new JsonObject();
// 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);
let responseStr: string;
try {
responseStr = await rest.fullRequestStringAsync('POST', '/2/files/list_folder', json.emit());
} catch {
console.log(rest.lastErrorText);
return;
}
// Success is indicated by a 200 response status code.
if (rest.responseStatusCode !== 200) {
// Examine the request/response to see what happened.
console.log(`response status code = ${rest.responseStatusCode}`);
console.log(`response status text = ${rest.responseStatusText}`);
console.log(`response header: ${rest.responseHeader}`);
console.log(`response body (if any): ${responseStr}`);
console.log('---');
console.log(`LastRequestStartLine: ${rest.lastRequestStartLine}`);
console.log(`LastRequestHeader: ${rest.lastRequestHeader}`);
return;
}
const jsonResponse = new JsonObject();
jsonResponse.load(responseStr);
jsonResponse.emitCompact = false;
console.log(jsonResponse.emit());
// A sample JSON response is shown at the end of this example.
// The following code iterates over the entries.
const dt = new DtObj();
const numEntries = jsonResponse.sizeOfArray('entries');
let i = 0;
while (i < numEntries) {
jsonResponse.i = i;
console.log('----');
console.log(`name: ${jsonResponse.stringOf('entries[i].name')}`);
console.log(`path_lower: ${jsonResponse.stringOf('entries[i].path_lower')}`);
console.log(`path_display: ${jsonResponse.stringOf('entries[i].path_display')}`);
if (jsonResponse.hasMember('entries[i].sharing_info')) {
console.log('has sharing_info...');
console.log(`read_only: ${jsonResponse.stringOf('entries[i].sharing_info.read_only')}`);
console.log(`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:
const ckdt = new 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.
const bLocalDateTime = true;
ckdt.toDtObj(bLocalDateTime, dt);
console.log(`${dt.year}-${dt.month}-${dt.day}`);
}
i++;
}
console.log('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
// }
//
}