Android Tutorial Dropbox SDK V2 - Authentification

DonManfred

Expert
Licensed User
This Tutorial belongs to the Dropbox SDK V2 - Java Library

See this thread for preparing your app to use this Library.
Especially the Manifest-Entry

=========================================================

To use Dropbox inside your App you need a accesstoken.

There are two ways to get a token to work with your or someone others Dropbox.

1. Inside the developer console at Dropbox you can generate a token and use this token then.
The token will be part of your apk and may be extracted easily.
You can use this way if you have an App which is not published on Playstore.
Maybe private apps for internal use only.

This token gives you access to YOUR Dropbox only!
The token will not expire except you generate a new token to use.




2. You ask the user to connect your App to his Dropbox.
To do this you need to call the Authentification-Flow from Dropbox.

A token you get with this way will not expire (sure you can revoke such a Token with a call to revoketoken in the DbxUserAuthRequests object. But usually you dont want to use it). I want to call it a "One-Time token for a User"

You can do it this way:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
  Dim auth As DbxAuth
   Dim Dropbox As DropboxV2
   Dim client As DbxClientV2
   Dim config As DbxRequestConfig
   Dim dbxFiles As DbxUserFilesRequests
   Dim dbxSharing As DbxUserSharingRequests
   Dim dbxUsers As DbxUserUsersRequests
   Dim token As String
   Dim DropboxEnabled As Boolean

End sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("OAuth")
    auth.Initialize("appkey")
End Sub
Then, to connect a Users Dropbox you start the Request from within a Button click event for ex.

B4X:
Sub btnStartOAuth_Click
    auth.startOAuth2Authentication
End Sub
A Dropbox Activity will appear and ask the User to login and then give your app the rights to use his Dropbox.

If he accept the request then DbxAuth´s OAuth2Token will be filled in Activity_resume

B4X:
Sub Activity_Resume
    If auth.OAuth2Token <> Null Then
        If auth.OAuth2Token <> "" Then
            token = auth.OAuth2Token
        End If
    End If
Once you got the token you should write the token down in a KVS or a Database to not loose it!

B4X:
            Starter.kvs.Put("token", token)
Now as we got the token we can start to Initialize Dropbox to use this token and get access to the users Dropbox based on the token. Additionally we finalize the initialisation of the different Dropbox Request-types.


B4X:
            Log("Token available. Dropbox enabled")
            config.Initialize("",token,"","de-de",5)
            Dim dbxhost As DbxHost
            dbxhost.Initialize
            client.Initialize("Dropbox",config,token,dbxhost)
            dbxFiles = client.files
            dbxFiles.setEventname("dbxFiles")
            dbxSharing = client.sharing
            dbxSharing.setEventname("dbxSharing")
            dbxUsers = client.users
            dbxUsers.setEventname("dbxUsers")
After this Dropbox should be ready initialized and you are ready to go



=====================================

If you later restart your app you need to check your kvs/database for an available token.
If not available then you need to get an Token first. See above.

If available then you directly do this Initialization-steps for Dropbox like this

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("OAuth")
    Dim dummy As String
    dummy = Starter.kvs.GetDefault("token", "")
    If dummy <> "" Then
        token = dummy
        lblToken.Text = token
        btnListroot.Enabled = True
        Log("Token available (Activity Create. Try to enable Dropbox with this token")
        config.Initialize("",token,"","de-de",5)
        Dim dbxhost As DbxHost
        dbxhost.Initialize
        client.Initialize("Dropbox",config,token,dbxhost)
        dbxFiles = client.files
        dbxFiles.setEventname("dbxFiles")
        dbxSharing = client.sharing
        dbxSharing.setEventname("dbxSharing")
        dbxUsers = client.users
        dbxUsers.setEventname("dbxUsers")
        DropboxEnabled = True
    Else
        DropboxEnabled = False
    End If
    Dropbox.Initialize("")
end sub
In this case you dont need to use the Auth-Flow to get a token (again). Just use the token you already got!

After this you should be ready to go!

Dropbox is initialized and all Request-Types are initialized too. DbxFileRequests, DbxSharingRequests, DbxUserRequests

You now can start Requesting File-Lists, doing Up- or Downloads...

More Dropbox Tutorials will come...
 
Top