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.basic4ppc.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,278
Last edited:

TommyE28

Member
Licensed User
Try to bild a wraper

@Erel,
I will try to build a dropbox-wraper. But here is the problem the librarys from the Dropbox-API was not found. Can you please take a look on my sources and the log.

Everything I have written so far is only the understanding of the wrapper for me to work and not in reality the implementation of future Dropbox API wrapper!

Sorry, my knowledge of Java is not very good.

Thank you for your efforts.
 

luke2012

Well-Known Member
Licensed User
Download specific file in app directory

Before of all, my compliments for this very interesting sample!

I understand that this sample works for upload files, but if I want to download a file from a defined dropbox direcory into a defined android app direcory which code can I use?

Thanks!
 

Tegwin

Member
Licensed User
Not working

Hello I have tried this but I always get the error messaage below when trying to run the program.. I copied the 2 OAuth files to the library as requested but I get the error all the time.. Any idea why ?

:sign0104:

Compiling code. Error
Error parsing program.
Error description: Unknown type: filedialog
Are you missing a library reference?
Occurred on line: 24
Dim FileDialog As FileDialog
 

Tegwin

Member
Licensed User
I have tried this example and I am getting the following error

Error: Status Code =401. You are using an older version of the Dropbox API with a newer API Key. Please use the latest version.

Any ideas how I get the latest version of this.
:sign0104:
 

Tegwin

Member
Licensed User
When I login I get an error saying

This app does not have permission for this operation..I see that there are some other people with the same error that never really seemed to be resolved.

Any ideas ?
 

thedesolatesoul

Expert
Licensed User
When I login I get an error saying

This app does not have permission for this operation..I see that there are some other people with the same error that never really seemed to be resolved.

Any ideas ?
I just told you the two things you need to change it to make it work.
I have been using the new API for more than 6 months now with no issues.
The permissions issue is because in your Dropbox app, it is only configured for sandbox (folder access) rather than full dropbox access.
 

thedesolatesoul

Expert
Licensed User

Xardoz

Member
Licensed User
I am new to B4A, and have installed the Dropbox demo project
Got my information from Dropbox
Put in my keys

But when I run it, When I attempt to log in, i get:
** Service (httputilsservice) Start **
An error occurred:
(Line: 34) End Sub
java.lang.Exception: Sub service_start signature does not match expected signature.
public static anywheresoftware.b4a.pc.RemoteObject anywheresoftware.b4a.samples.dropbox.httputilsservice_subs_0._service_start() throws java.lang.Exception

Line 34 is the End Sub of this proceedure
B4X:
Sub Service_Create
    If TempFolder = "" Then TempFolder = File.DirInternalCache
    If OAuth.IsInitialized = False Then
        OAuth.Initialize(Main.developerKey, Main.developerSecret)
    End If
    If hcIsInitialized = False Then
        hc.Initialize("hc")
        hcIsInitialized = True
    End If
End Sub

You assistance is appreciated.
 
Top