Using Dropbox to save files

From NSB App Studio
Jump to navigation Jump to search

Dropbox has a new API which is documented on their site. As a result, this library has been deprecated.

It's easy to save and read files on Dropbox using AppStudio. You can then access them on all your devices and on your desktop.

  • Apps must be set up in Dropbox before they will run.
  • Apps need to be deployed. They cannot run locally.
  • Apps using PhoneGap will need a different procedure to do OAuth than described here.
  • Your site will need a signing certificate so OAuth will work.
  • You can only run someone else's app if you have their permission.
  • Therefore you can only access someone else's files with their permission.
  • For PhoneGap apps, see this blog post.

Here's how:

Get a Dropbox account.

Use your existing account or get a new one. They're free and easy to set up.

Create your app in the Dropbox App Console

Go to Dropbox App Console and create a Dropbox API app, that does "Files and data stores" that it creates. It's also possible to access existing files in the Dropbox by choosing appropriate options here.




Settings Fields:

  • Status: Development allows up to 100 users. To apply for Production, your app will have to approved by Dropbox.
  • Development users: Who is allowed to use the app?
  • Permission type: Normally, read and write to its own folder.
  • App key, App secret: Generated by Dropbox. Use App key in your app to identify the Dropbox app.
  • OAuth redirect URIs: The complete URL to your deployed app.
  • Drop-ins domains: Not needed.
  • Datastores: Not needed.



Details Fields:

  • App name: The name of your app. Don't use "Dropbox" in the name.
  • Publisher: Your name or the name of your company.
  • Description: A description of your app.
  • App website: The main website for your app.
  • App icons: 64x64 and 256x256 images. You need to save them to your Dropbox before uploading them.

Write your app

Add the Dropbox library to your project

Select Global Code and Project Properties in the Project Explorer, and add the Dropbox library to the project:


Log into Dropbox

Security is important to Dropbox apps. Before you can do anything, you need to login. Dropbox uses a security schema call OAuth, which is fairly ease to use. OAuth requires that you use https: your site site will need a signing certificate. You can install a self signed certificate (browsers will complain) or an official one (about $59/year from a certificate authority).

Here is what you do at the start of your app:

Dim APP_KEY = "qx65sf9nruudj73"
Dim client = new Dropbox.Client({key: APP_KEY})
client.authenticate({interactive:False}, authenticationError)

Function authenticationError(err)
  If err Then MsgBox "Dropbox Authentication error: " & err
End Function

Once this is done, you can check to see if the user is authorized. If not, you need to authenticate:


This will load the Dropbox web page, ask the user to log in if needed and get authorization for the app.

Write a file to your Dropbox

Once authenticated, writing files out is easy.

s="This is my data"
client.writeFile("MyData.txt", s, {}, writeComplete)

Function writeComplete(err)
  MsgBox "Data written. Error: " & err
End Function
  • The file will be created in /Dropbox/Apps/yourAppName
  • You can use any extension.
  • It's your responsibility to format the data properly for the extension you use.
  • If the file exists, it is overwritten by default.
  • To prevent overwrite, put {noOverwrite: true} into the 3rd parameter.
  • Data to be written out can be quite large.
  • JSON is a nice format to use for structured data. Use JSON.Stringify and JSON.Parse.
  • writeFile executes asynchronously.
  • The last parameter is the function to call on completion of write.
  • An error code is passed to the writeComplete function.
  • If operation is successful, null is passed.

Read a file from your Dropbox

Reading is as easy as writing.

client.readFile("MyData.txt", {}, readComplete)  

Function readComplete(err, data)
  MsgBox "Data read: " & data
End Function
  • The file will be read from /Dropbox/Apps/yourAppName
  • JSON is a nice format to use for structured data. Use JSON.Stringify and JSON.Parse.
  • readFile executes asynchronously.
  • The last parameter is the function to call on completion of write.
  • An error code and the data is passed to the readComplete function.
  • If operation is successful, null is passed as err.
  • There are additional options for data type, start position and number of bytes to read.

Other Dropbox file commands

Here is the full list of calls that can be made to the Dropbox client. Docs are here.

  • signOut(options, callback)
  • getAccountInfo(options, callback)
  • readFile(path, options, callback)
  • writeFile(path, data, options, callback)
  • resumableUploadStep(data, cursor, callback)
  • resumableUploadFinish(path, options, callback)
  • stat(path, options, callback)
  • readdir(path, options, callback)
  • makeUrl(path, options, callback)
  • history(path, options, callback)
  • readThumbnail(path, options, callback)
  • revertFile(path, versionTag, callback)
  • findByName(path, namePattern, options, callback)
  • makeCopyReference(path, callback)
  • pullChanges(cursor, callback)
  • mkdir(path, callback)
  • remove(path, callback)
  • copy(from, toPath, callback)
  • move(fromPath, toPath, callback)
  • appInfo(appKey, callback)
  • isAppDeveloper(userId, appKey, callback)
  • hasOauthRedirectUri(redirectUri, appKey, callback)

"Dropbox Cannot Authenticate" message

If your app should get a message like this after working for a little while, you may need to clean the cache on your computer or device. This is more likely to happen during development than in released apps.