Search code examples
androiddropboxdropbox-api

Dropbox API on Android - Using Token to download file from Apps folder


I generated an access token to be able to make API calls for my own account without going through the authorization flow. I found this Dropbox files Get API but I don't know how to use it.

I tried this code, but it doesn't seem to work:

    // Authentication with Token
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().setOAuth2AccessToken(ACCESS_TOKEN);

    // Upload a file to Apps folder
    File file = new File("working-draft.txt");

    FileInputStream inputStream = null;
    try {
        inputStream = new FileInputStream(file);
        DropboxAPI.Entry response = mDBApi.putFile("/magnum-opus.txt", inputStream,
                file.length(), null, null);
        Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev);

    } catch (Exception e) {
        e.printStackTrace();
    }

How can I upload and download directly to the Apps folder using the token key? Also is there a way to print the list of all the files in my Apps folder?


Solution

  • The docs are pretty poor. I found the following examples on Github which helped me: https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android/src/main/java/com/dropbox/core/examples/android

    In gradle compile 'com.dropbox.core:dropbox-core-sdk:3.0.2' or whatever is the latest

    The key and secret are written into a JSON file + there's an entry you need to add into the manifest with the app key. Just follow the example which shows placeholders.

    Once you've done the handshake and got the access token back

    DbxRequestConfig requestConfig = DbxRequestConfig.newBuilder("your identifier")
                .withHttpRequestor(new 
    OkHttp3Requestor(OkHttp3Requestor.defaultOkHttpClient()))
                .build();
    
    dbxClient = new DbxClientV2(requestConfig, accessToken);
    
    dbxClient.files().[operation e.g. upload\download\file listing]