Android Tutorial Android Dropbox / OAuth Tutorial

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:

dropbox_1.png


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)
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:

dropbox_2.png


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

  • Dropbox.zip
    12.6 KB · Views: 2,479
Last edited:

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi
i run project but error "Error: StatusCode=400, version 0 of the API is not supported"
what's problem?
 

johnB

Active Member
Licensed User
Longtime User
Hi, I'm trying to connect to a site that has OAuth2.0 working but they haven't finished their documentation. So that I can ask them the right questions:

Can anyone please tell me what the "scope" string represents

They have given me an Authorization Token and an Access Token but neither of these seem to work.

Also what is the significance or the Redirect URI. They have given me an address that is registered in my name but I don't do anything with it except using it for a test Web Site

Thanks in anticipation

PS I have the GMail Contacts app working
 

sultan87

Active Member
Licensed User
Longtime User
OK thanks
I have an app developed with basic4android that uses SQLite bd
I would like it to be accéssible from my smartphone or my tablet
how to do?
best regards
 
Top