This tutorial is based on an old version of Dropbox API. For new projects it is recommended to use the Dropbox Sync library:
http://www.b4x.com/android/forum/threads/30112
Dropbox is a service that allows you to share and synchronize files. Dropbox offers an API which can be used to integrate Dropbox services in your own applications. The API is described here.
OAuth standard allows users to share private information with a third party application or site without giving their credentials, and in a manageable way.
This example project allows the user to browse their online Dropbox files, download files and upload files.
Two steps are required in order to get access to the user private data. First we as the developers should register with Dropbox and get our personal developer key / secret.
The second step is to get the access token. This is done by sending a request to Dropbox with the user credentials. The server should respond with the token key / secret. This token should be saved for future requests. You are not allowed to store the user credentials.
When our application starts we check if we already have the token saved in an internal file. If not we ask the user for their username and password:
Using the new OAuth library we are signing all the Http requests before sending them. Signing the request adds a header named "Authorization". The signature depends on the request values.
Signing is done by calling:
The OAuth object is initialized with the developer key and secret. Later when we acquire the access token we call OAuth.SetTokenWithSecret.
Note that the OAuth library wraps the oauth-signpost open source project.
Both the developer key/secret and the token are used during signing.
In this example are using HttpUtils to manage the Http calls. HttpUtils was modified to support signing as well as the file uploading method which required special handling.
Once we have the access token we call ChangePath.
This sends a "metadata" call to Dropbox. The response contains a list with the files and folders:
When the user presses on a folder we again call ChangePath and show the contents of the folder.
Each time we open a folder, the server response is saved in a Map (FilesCache). Later when a folder is reopened the data is retrieved from this cache.
Some characters must be encoded before they can be used in a Url. This is done with the help of StringUtils.EncodeUrl.
We use FileDialog which is part of the Dialogs library to let the user choose a file to upload. Note that the current implementation stores the whole file in memory before uploading it. It will not work for very large files.
Not all of the Dropbox API methods are implemented. However it should be pretty simple to add the other methods based on the existing implementation.
As the communication done with a service, everything should work correctly even if the user closes the application in the middle of a file transfer. Check Sub Activity_Resume to see how the transfers are managed.
In order to run the example you should first set developerKey and developerSecret variables (in the main activity).
http://www.b4x.com/android/forum/threads/30112
Dropbox is a service that allows you to share and synchronize files. Dropbox offers an API which can be used to integrate Dropbox services in your own applications. The API is described here.
OAuth standard allows users to share private information with a third party application or site without giving their credentials, and in a manageable way.
This example project allows the user to browse their online Dropbox files, download files and upload files.
Two steps are required in order to get access to the user private data. First we as the developers should register with Dropbox and get our personal developer key / secret.
The second step is to get the access token. This is done by sending a request to Dropbox with the user credentials. The server should respond with the token key / secret. This token should be saved for future requests. You are not allowed to store the user credentials.
When our application starts we check if we already have the token saved in an internal file. If not we ask the user for their username and password:
Using the new OAuth library we are signing all the Http requests before sending them. Signing the request adds a header named "Authorization". The signature depends on the request values.
Signing is done by calling:
B4X:
OAuth.Sign(Request)
Note that the OAuth library wraps the oauth-signpost open source project.
Both the developer key/secret and the token are used during signing.
In this example are using HttpUtils to manage the Http calls. HttpUtils was modified to support signing as well as the file uploading method which required special handling.
Once we have the access token we call ChangePath.
This sends a "metadata" call to Dropbox. The response contains a list with the files and folders:
When the user presses on a folder we again call ChangePath and show the contents of the folder.
Each time we open a folder, the server response is saved in a Map (FilesCache). Later when a folder is reopened the data is retrieved from this cache.
Some characters must be encoded before they can be used in a Url. This is done with the help of StringUtils.EncodeUrl.
We use FileDialog which is part of the Dialogs library to let the user choose a file to upload. Note that the current implementation stores the whole file in memory before uploading it. It will not work for very large files.
Not all of the Dropbox API methods are implemented. However it should be pretty simple to add the other methods based on the existing implementation.
As the communication done with a service, everything should work correctly even if the user closes the application in the middle of a file transfer. Check Sub Activity_Resume to see how the transfers are managed.
In order to run the example you should first set developerKey and developerSecret variables (in the main activity).
Attachments
Last edited: