PowerBuilder
PowerBuilder
Download File from Dropbox
See more Dropbox Examples
Downloads a file from Dropbox.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Rest
oleobject loo_Json
oleobject loo_Bd
string ls_ApiResult
oleobject loo_JsonResult
integer li_Size
string ls_Rev
string ls_ClientModified
oleobject loo_Ckdt
integer li_BLocalTime
oleobject loo_Dt
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// A Dropbox access token should have been previously obtained.
// Dropbox access tokens do not expire.
// See Dropbox Access Token.
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
if li_rc < 0 then
destroy loo_Rest
MessageBox("Error","Connecting to COM object failed")
return
end if
// Connect to Dropbox
li_Success = loo_Rest.Connect("content.dropboxapi.com",443,1,1)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
return
end if
// Add request headers.
loo_Rest.AddHeader("Authorization","Bearer DROPBOX_ACCESS_TOKEN")
// The download "parameters" are contained in JSON passed in an HTTP request header.
// This is the JSON indicating the file to be downloaded:
// {
// "path": "/Homework/lit/hamlet.xml",
// }
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
loo_Json.AppendString("path","/Homework/lit/hamlet.xml")
loo_Rest.AddHeader("Dropbox-API-Arg",loo_Json.Emit())
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")
li_Success = loo_Rest.FullRequestNoBodyBd("POST","/2/files/download",loo_Bd)
if li_Success = 0 then
Write-Debug loo_Rest.LastErrorText
destroy loo_Rest
destroy loo_Json
destroy loo_Bd
return
end if
// When successful, Dropbox responds with a 200 response code.
if loo_Rest.ResponseStatusCode <> 200 then
// Examine the request/response to see what happened.
Write-Debug "response status code = " + string(loo_Rest.ResponseStatusCode)
Write-Debug "response status text = " + loo_Rest.ResponseStatusText
Write-Debug "response header: " + loo_Rest.ResponseHeader
Write-Debug "response body (if any): " + loo_Bd.GetString("utf-8")
Write-Debug "---"
Write-Debug "LastRequestStartLine: " + loo_Rest.LastRequestStartLine
Write-Debug "LastRequestHeader: " + loo_Rest.LastRequestHeader
destroy loo_Rest
destroy loo_Json
destroy loo_Bd
return
end if
// The file was downloaded into bd.
// Save it to the filesystem.
li_Success = loo_Bd.WriteFile("c:/temp/qa_output/hamlet.xml")
// Information about the downloaded file is also available as JSON in a response header.
// The "dropbox-api-result" response header contains the information. For example:
ls_ApiResult = loo_Rest.ResponseHdrByName("dropbox-api-result")
Write-Debug ls_ApiResult
// In this case, the pretty-formatted dropbox-api-result JSON looks like this:
// {
// "name": "hamlet.xml",
// "path_lower": "/homework/lit/hamlet.xml",
// "path_display": "/Homework/lit/hamlet.xml",
// "id": "id:74FkdeNuyKAAAAAAAAAAAQ",
// "client_modified": "2016-06-02T23:19:00Z",
// "server_modified": "2016-06-02T23:19:00Z",
// "rev": "9482db15f",
// "size": 279658
// }
// Load the JSON, pretty-print it, and demonstrate how to get some values...
loo_JsonResult = create oleobject
li_rc = loo_JsonResult.ConnectToNewObject("Chilkat.JsonObject")
loo_JsonResult.EmitCompact = 0
loo_JsonResult.Load(ls_ApiResult)
// Show the JSON pretty-printed...
Write-Debug loo_JsonResult.Emit()
// Sample code to get data from the JSON response:
li_Size = loo_JsonResult.IntOf("size")
Write-Debug "size = " + string(li_Size)
ls_Rev = loo_JsonResult.StringOf("rev")
Write-Debug "rev = " + ls_Rev
ls_ClientModified = loo_JsonResult.StringOf("client_modified")
loo_Ckdt = create oleobject
li_rc = loo_Ckdt.ConnectToNewObject("Chilkat.CkDateTime")
loo_Ckdt.SetFromTimestamp(ls_ClientModified)
li_BLocalTime = 1
loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.DtObj")
loo_Ckdt.ToDtObj(li_BLocalTime,loo_Dt)
Write-Debug string(loo_Dt.Day) + "/" + string(loo_Dt.Month) + "/" + string(loo_Dt.Year) + " " + string(loo_Dt.Hour) + ":" + string(loo_Dt.Minute)
destroy loo_Rest
destroy loo_Json
destroy loo_Bd
destroy loo_JsonResult
destroy loo_Ckdt
destroy loo_Dt