Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkRestW.h>
#include <CkJsonObjectW.h>
#include <CkDtObjW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkRestW rest;
// Connect to the www.dropbox.com endpoint.
bool bTls = true;
int port = 443;
bool bAutoReconnect = true;
success = rest.Connect(L"api.dropboxapi.com",port,bTls,bAutoReconnect);
if (success == false) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
rest.AddHeader(L"Content-Type",L"application/json");
rest.AddHeader(L"Authorization",L"Bearer DROPBOX-ACCESS-TOKEN");
CkJsonObjectW json;
// The root folder should be an empty string, not "/"
json.AppendString(L"path",L"");
json.AppendBool(L"recursive",false);
json.AppendBool(L"include_media_info",false);
json.AppendBool(L"include_deleted",false);
json.AppendBool(L"include_has_explicit_shared_members",false);
const wchar_t *responseStr = rest.fullRequestString(L"POST",L"/2/files/list_folder",json.emit());
if (rest.get_LastMethodSuccess() == false) {
wprintf(L"%s\n",rest.lastErrorText());
return;
}
// Success is indicated by a 200 response status code.
if (rest.get_ResponseStatusCode() != 200) {
// Examine the request/response to see what happened.
wprintf(L"response status code = %d\n",rest.get_ResponseStatusCode());
wprintf(L"response status text = %s\n",rest.responseStatusText());
wprintf(L"response header: %s\n",rest.responseHeader());
wprintf(L"response body (if any): %s\n",responseStr);
wprintf(L"---\n");
wprintf(L"LastRequestStartLine: %s\n",rest.lastRequestStartLine());
wprintf(L"LastRequestHeader: %s\n",rest.lastRequestHeader());
return;
}
CkJsonObjectW jsonResponse;
jsonResponse.Load(responseStr);
jsonResponse.put_EmitCompact(false);
wprintf(L"%s\n",jsonResponse.emit());
// A sample JSON response is shown at the end of this example.
// The following code iterates over the entries.
CkDtObjW dt;
int numEntries = jsonResponse.SizeOfArray(L"entries");
int i = 0;
while (i < numEntries) {
jsonResponse.put_I(i);
wprintf(L"----\n");
wprintf(L"name: %s\n",jsonResponse.stringOf(L"entries[i].name"));
wprintf(L"path_lower: %s\n",jsonResponse.stringOf(L"entries[i].path_lower"));
wprintf(L"path_display: %s\n",jsonResponse.stringOf(L"entries[i].path_display"));
if (jsonResponse.HasMember(L"entries[i].sharing_info") == true) {
wprintf(L"has sharing_info...\n");
wprintf(L"read_only: %s\n",jsonResponse.stringOf(L"entries[i].sharing_info.read_only"));
wprintf(L"shared_folder_id: %s\n",jsonResponse.stringOf(L"entries[i].sharing_info.shared_folder_id"));
}
if (jsonResponse.HasMember(L"entries[i].client_modified") == true) {
// Demonstrate how to parse a date/time:
CkDateTimeW ckdt;
success = ckdt.SetFromTimestamp(jsonResponse.stringOf(L"entries[i].client_modified"));
// The date/time can now be converted to many other formats, or the individual parts
// can be accessed.
bool bLocalDateTime = true;
ckdt.ToDtObj(bLocalDateTime,dt);
wprintf(L"%d-%d-%d\n",dt.get_Year(),dt.get_Month(),dt.get_Day());
}
i = i + 1;
}
wprintf(L"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
// }
//
}