Praveen
Praveen

Reputation: 393

Dropbox Core and Sync APIs together in Android App

Is it possible to use both the Core and Sync Api in one Android app?

Upvotes: 0

Views: 55

Answers (1)

Praveen
Praveen

Reputation: 393

It is possible to use them together. It's a 2 part setup.

Removing Project errors:

  1. Add jar files both SDKs to your project
  2. Now open Dropbox Core SDK jar file and remove the client2.Auth classes - anything that's causing a namespace collision. You will see errors until this is fixed

Authenticating the SDKs:

  1. Setup dropbox linking for the Sync SDK - there are many docs on this
  2. Get the oAuth credentials from Sync SDK for your Core SDK using:

      AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
      AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
      session.setOAuth2AccessToken(getTokenFromSyncAPI());
      session.finishAuthentication();
    

And finally, the missing method:

String getTokenFromSyncAPI() {
    String token = null;

    String allTokens = getApplicationContext().getSharedPreferences("dropbox-credentials",
            Context.MODE_PRIVATE).getString("accounts", null);

    try {
        JSONArray jsonAccounts = new JSONArray(allTokens);
        if (jsonAccounts.length() > 0) {
            String tmpToken = null;
            tmpToken = jsonAccounts.getJSONObject(0).getString("userToken");

            // We take only oAuth2 tokens
            if (tmpToken.startsWith("|oa2|"))
                token = tmpToken.substring(5);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return token;
}

Method courtesy : https://blogs.dropbox.com/developers/2015/05/migrating-sync-sdk-access-tokens-to-core-sdk/

PS : The method shown at the link has a bug. substring(6) instead of 5

Upvotes: 1

Related Questions