Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Dropbox Android Example

This is a sample app to show you how to integrate an Android app with the Dropbox Java SDK. It demonstrates how to do the following:

  • Authenticates with the Dropbox SDK
  • Upload a photo to Dropbox
  • Fetch the contents of a folder
  • Fetch a user's profile information
  • Use Kotlin Coroutines to make calls to the Dropbox API off the main thread using the Java SDK
  • Hide the app key using local.properties (as described below)

To run the app locally you'll need to create a Dropbox app key in the developer console and put this line in the local.properties file: DROPBOX_KEY=mydropboxkeygoeshere

Creating your own Android app with the Dropbox Java SDK

  1. Acquire an app key for your project, instructions here
  2. Add the dependency to your build.gradle with the latest version. You can find our versions here.
implementation 'com.dropbox.core:dropbox-core-sdk:X.Y.Z'
  1. Add the following to your AndroidManifest.xml and replace YOUR_APP_KEY with the key from the console
        <activity
            android:name="com.dropbox.core.android.AuthActivity"
            android:configChanges="orientation|keyboard"
            android:launchMode="singleTask">
            <intent-filter>

                <!-- Change this to be db- followed by your app key -->
                <data android:scheme="db-YOUR_APP_KEY" />

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
  1. To initiate the Dropbox authentication flow call startOAuth2PKCE with the appropiate scopes your app will need. Read more about scopes here. This will launch the Dropbox app if installed or Dropbox.com in the browser.
val scopes = listOf("account_info.read", "files.content.write")
Auth.startOAuth2PKCE(this, APP_KEY, requestConfig, scopes)
  1. When the user has completed the authentication flow they will be taken back to your app. In onResume you can call Auth.getDbxCredential() to get the resulting DbxCredential.
  2. To make requests using the Dropbox Java SDK, create a DbxClientV2 like so. ClientIdentifiers are usually of the form "SoftwareName/SoftwareVersion". DbxClientV2 will have the request objects needed to make a call to the Dropbox API.
val clientIdentifier = "DropboxSampleAndroid/1.0.0"
val requestConfig = DbxRequestConfig(clientIdentifier)
val dropboxClient = DbxClientV2(requestConfig, credential)

The Dropbox Java SDK will use the long lived refresh token found in the DbxCredential to automatically refresh your short lived access token when it makes requests.

Hiding your Dropbox App Secret

If you want to hide your Dropbox app secret, you can follow these steps:

  1. Edit your local.properties file to contain the following line with your Dropbox key from the developer console
DROPBOX_KEY=mydropboxkeygoeshere
  1. Edit your build.gradle to include the following:
android {
    defaultConfig {
        //more config

        Properties localProperties = getLocalProperties()
        String dropboxKey = localProperties['DROPBOX_KEY']
        buildConfigField "String", "DROPBOX_KEY", "\"${dropboxKey}\""
        manifestPlaceholders = [dropboxKey: dropboxKey]
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

def getLocalProperties() {
    Properties props = new Properties()
    if (file('../local.properties').exists()) {
        props.load(new FileInputStream(file('../local.properties')))
    }
    return props
}
  1. Edit your AndroidManifest.xml to use the manifest placeholder defined in step 2.
    <activity
        android:name="com.dropbox.core.android.AuthActivity"
        android:configChanges="orientation|keyboard"
        android:launchMode="singleTask">
        <intent-filter>
            <!-- Using a Manifest Placeholder to hide our app key -->
            <data android:scheme="db-${dropboxKey}" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
  1. Use your app key with:
val APP_KEY = BuildConfig.DROPBOX_KEY

Authentication with the Dropbox Java SDK

Starting September 30, 2021 the Dropbox authentication flow will only return short lived tokens. The Java SDK has been updated to support this using DbxCredentials. This credential consists of a short lived accessToken and a long lived refreshToken. The SDK will refresh this short lived access token by calling the server and exchanging the refresh token for an access token when the access token has expired (See refreshAccessTokenIfNeeded() in DbxRawClientV2). This allows clients to continue to have a valid access token without further interaction with the user. This is useful for mobile clients that need to do periodic background work without having to force the user to manually re-authenticate. You can read more about this on the Dropbox Tech blog

If you have users with existing long lived access tokens, they will not be impacted. Only tokens issued after September 30th will be short lived.