Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Dropbox: Access Your Own AccountTo 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.
#include <C_CkRestW.h> #include <C_CkJsonObjectW.h> #include <C_CkDateTimeW.h> #include <C_CkDtObjW.h> void ChilkatSample(void) { HCkRestW rest; BOOL bTls; int port; BOOL bAutoReconnect; BOOL success; HCkJsonObjectW json; const wchar_t *responseStr; HCkJsonObjectW jsonResponse; int numEntries; int i; HCkDateTimeW ckdt; BOOL bLocalDateTime; HCkDtObjW dt; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. rest = CkRestW_Create(); // Connect to the www.dropbox.com endpoint. bTls = TRUE; port = 443; bAutoReconnect = TRUE; success = CkRestW_Connect(rest,L"api.dropboxapi.com",port,bTls,bAutoReconnect); if (success != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkRestW_Dispose(rest); return; } CkRestW_AddHeader(rest,L"Content-Type",L"application/json"); CkRestW_AddHeader(rest,L"Authorization",L"Bearer DROPBOX-ACCESS-TOKEN"); json = CkJsonObjectW_Create(); // The root folder should be an empty string, not "/" CkJsonObjectW_AppendString(json,L"path",L""); CkJsonObjectW_AppendBool(json,L"recursive",FALSE); CkJsonObjectW_AppendBool(json,L"include_media_info",FALSE); CkJsonObjectW_AppendBool(json,L"include_deleted",FALSE); CkJsonObjectW_AppendBool(json,L"include_has_explicit_shared_members",FALSE); responseStr = CkRestW_fullRequestString(rest,L"POST",L"/2/files/list_folder",CkJsonObjectW_emit(json)); if (CkRestW_getLastMethodSuccess(rest) != TRUE) { wprintf(L"%s\n",CkRestW_lastErrorText(rest)); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(json); return; } // Success is indicated by a 200 response status code. if (CkRestW_getResponseStatusCode(rest) != 200) { // Examine the request/response to see what happened. wprintf(L"response status code = %d\n",CkRestW_getResponseStatusCode(rest)); wprintf(L"response status text = %s\n",CkRestW_responseStatusText(rest)); wprintf(L"response header: %s\n",CkRestW_responseHeader(rest)); wprintf(L"response body (if any): %s\n",responseStr); wprintf(L"---\n"); wprintf(L"LastRequestStartLine: %s\n",CkRestW_lastRequestStartLine(rest)); wprintf(L"LastRequestHeader: %s\n",CkRestW_lastRequestHeader(rest)); CkRestW_Dispose(rest); CkJsonObjectW_Dispose(json); return; } jsonResponse = CkJsonObjectW_Create(); CkJsonObjectW_Load(jsonResponse,responseStr); CkJsonObjectW_putEmitCompact(jsonResponse,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(jsonResponse)); // A sample JSON response is shown at the end of this example. // The following code iterates over the entries. numEntries = CkJsonObjectW_SizeOfArray(jsonResponse,L"entries"); i = 0; while (i < numEntries) { CkJsonObjectW_putI(jsonResponse,i); wprintf(L"----\n"); wprintf(L"name: %s\n",CkJsonObjectW_stringOf(jsonResponse,L"entries[i].name")); wprintf(L"path_lower: %s\n",CkJsonObjectW_stringOf(jsonResponse,L"entries[i].path_lower")); wprintf(L"path_display: %s\n",CkJsonObjectW_stringOf(jsonResponse,L"entries[i].path_display")); if (CkJsonObjectW_HasMember(jsonResponse,L"entries[i].sharing_info") == TRUE) { wprintf(L"has sharing_info...\n"); wprintf(L"read_only: %s\n",CkJsonObjectW_stringOf(jsonResponse,L"entries[i].sharing_info.read_only")); wprintf(L"shared_folder_id: %s\n",CkJsonObjectW_stringOf(jsonResponse,L"entries[i].sharing_info.shared_folder_id")); } if (CkJsonObjectW_HasMember(jsonResponse,L"entries[i].client_modified") == TRUE) { // Demonstrate how to parse a date/time: ckdt = CkDateTimeW_Create(); success = CkDateTimeW_SetFromTimestamp(ckdt,CkJsonObjectW_stringOf(jsonResponse,L"entries[i].client_modified")); // The date/time can now be converted to many other formats, or the individual parts // can be accessed. bLocalDateTime = TRUE; dt = CkDateTimeW_GetDtObj(ckdt,bLocalDateTime); wprintf(L"%d-%d-%d\n",CkDtObjW_getYear(dt),CkDtObjW_getMonth(dt),CkDtObjW_getDay(dt)); CkDtObjW_Dispose(dt); } 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 // } // CkRestW_Dispose(rest); CkJsonObjectW_Dispose(json); CkJsonObjectW_Dispose(jsonResponse); CkDateTimeW_Dispose(ckdt); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.