Using Dropbox to save files
It's easy to save and read files on Dropbox using AppStudio, so you can access them on all your devices and on your desktop.
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.
Settings
Settings Fields:
- Status: Development allows up to 100 users. To apply for Production, your app will have to approved by Dropbox.
- Developments users: Who is allowed to use the app?
- Permission type: Normally, read and write to its own folder.
- App Folder Name: Name the folder you want to use in Dropbox/Apps for your app's files. If it does not exist, it will be created.
- 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, including index.html.
- Drop-ins domains: Not needed.
- Datastores: Not needed.
Details
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
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.
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:
client.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 in /DropBox/Apps/yourAppName
- If the file exists, it is overwritten by default.
- 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)