Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = CkoRest()!
// Connect to the www.dropbox.com endpoint.
var bTls: Bool = true
var port: Int = 443
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "api.dropboxapi.com", port: port, tls: bTls, autoReconnect: bAutoReconnect)
if success == false {
print("\(rest.lastErrorText!)")
return
}
rest.addHeader(name: "Content-Type", value: "application/json")
rest.addHeader(name: "Authorization", value: "Bearer DROPBOX-ACCESS-TOKEN")
let json = CkoJsonObject()!
// The root folder should be an empty string, not "/"
json.appendString(name: "path", value: "")
json.appendBool(name: "recursive", value: false)
json.appendBool(name: "include_media_info", value: false)
json.appendBool(name: "include_deleted", value: false)
json.appendBool(name: "include_has_explicit_shared_members", value: false)
var responseStr: String? = rest.fullRequestString(httpVerb: "POST", uriPath: "/2/files/list_folder", bodyText: json.emit())
if rest.lastMethodSuccess == false {
print("\(rest.lastErrorText!)")
return
}
// Success is indicated by a 200 response status code.
if rest.responseStatusCode.intValue != 200 {
// Examine the request/response to see what happened.
print("response status code = \(rest.responseStatusCode.intValue)")
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
}
let jsonResponse = CkoJsonObject()!
jsonResponse.load(json: 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.
let dt = CkoDtObj()!
var numEntries: Int = jsonResponse.size(ofArray: "entries").intValue
var i: Int = 0
while i < numEntries {
jsonResponse.i = i
print("----")
print("name: \(jsonResponse.string(of: "entries[i].name")!)")
print("path_lower: \(jsonResponse.string(of: "entries[i].path_lower")!)")
print("path_display: \(jsonResponse.string(of: "entries[i].path_display")!)")
if jsonResponse.hasMember(jsonPath: "entries[i].sharing_info") == true {
print("has sharing_info...")
print("read_only: \(jsonResponse.string(of: "entries[i].sharing_info.read_only")!)")
print("shared_folder_id: \(jsonResponse.string(of: "entries[i].sharing_info.shared_folder_id")!)")
}
if jsonResponse.hasMember(jsonPath: "entries[i].client_modified") == true {
// Demonstrate how to parse a date/time:
let ckdt = CkoDateTime()!
success = ckdt.set(fromTimestamp: jsonResponse.string(of: "entries[i].client_modified"))
// The date/time can now be converted to many other formats, or the individual parts
// can be accessed.
var bLocalDateTime: Bool = true
ckdt.toDtObj(bLocal: bLocalDateTime, dtObj: dt)
print("\(dt.year.intValue)-\(dt.month.intValue)-\(dt.day.intValue)")
}
i = i + 1
}
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
// }
//
}