0

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?

João Cartucho
  • 3,688
  • 32
  • 39
  • 1
    Note that the code in your question is using the [deprecated](https://blogs.dropbox.com/developers/2016/06/api-v1-deprecated/) [Dropbox API v1 SDK](https://www.dropbox.com/developers-v1/core/sdks/android). You should instead use the [Dropbox API v2 Java SDK](https://github.com/dropbox/dropbox-sdk-java) as shown in [Pete's answer](http://stackoverflow.com/a/43985574/1305693). – Greg May 15 '17 at 19:34

1 Answers1

2

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]
Pete Hendry
  • 291
  • 2
  • 7
  • There's also [example code for uploading](https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/upload-file/src/main/java/com/dropbox/core/examples/upload_file/Main.java) in particular. – Greg May 15 '17 at 19:35
  • Thank you Pete Hendry and @Greg ! I will try the code and let you know if it worked. Indeed the docs are very poor! – João Cartucho May 15 '17 at 23:28
  • Pete what do you mean by 'your identifier' when setting up the config? – João Cartucho May 16 '17 at 13:55
  • Pete and @Greg, I added a second attempt code to the question. I used the API v2. docs as you said and I managed to get the files listing working fine! Now the download / upload part is still not working and I don't know why – João Cartucho May 16 '17 at 14:17
  • @JoãoCartucho 'your identifier' can be any string as far as I'm aware. To fix upload you need to pass a file path to the uploadBuidler where you want to save the file on Dropbox e.g. FileMetadata metadata = client.files().uploadBuilder("/test.txt") .uploadAndFinish(in); You can also pass a directory structure e.g. /folder/test.txt – Pete Hendry May 16 '17 at 14:30
  • I tried with that but I still get the same problem, it seems like my token only as access to list the files and not to upload / download, but I'm not sure if that is even possible – João Cartucho May 16 '17 at 14:49
  • Hmm, does sound strange. I've only tried upload\download not file listing. I'm assuming you're getting the Dropbox access token correctly if you can access the file listing. Remember that when you upload if you're using the App folder the file will go under Apps// – Pete Hendry May 16 '17 at 15:13
  • Dropbox API access tokens can always both list and upload/download files. There isn't a metadata-only access or anything like that. For the current issue, are you getting any error or output? It sounds like your upload/download code may be hanging on the network calls. Is there anything blocking your access to content.dropboxapi.com? – Greg May 16 '17 at 18:11