在onCreate()中写
AppKeyPair pair = new AppKeyPair(ACCESS_KEY, ACCESS_SECRET);
session = new AndroidAuthSession(pair, AccessType.APP_FOLDER);
dropbox = new DropboxAPI(session);
SharedPreferences prefs = getSharedPreferences(DROPBOX_NAME, 0);
String key = prefs.getString(ACCESS_KEY, null);
String secret = prefs.getString(ACCESS_SECRET, null);
if (key != null && secret != null) {
Log.d("key secret", key + " " + secret);
AccessTokenPair token = new AccessTokenPair(key, secret);
dropbox.getSession().setAccessTokenPair(token);
}
if (key == null && secret == null)
dropbox.getSession().startAuthentication(DropboxActivity.this);
并在onResume()中写
if (dropbox.getSession().isLinked()) {
try {
loggedIn(true);
doAction();
} catch (IllegalStateException e) {
Toast.makeText(this, "Error during Dropbox authentication",
Toast.LENGTH_SHORT).show();
}
} else if (dropbox.getSession().authenticationSuccessful()) {
try {
session.finishAuthentication();
TokenPair tokens = session.getAccessTokenPair();
SharedPreferences prefs = getSharedPreferences(DROPBOX_NAME, 0);
Editor editor = prefs.edit();
editor.putString(ACCESS_KEY, tokens.key);
editor.putString(ACCESS_SECRET, tokens.secret);
editor.commit();
loggedIn(true);
doAction();
} catch (IllegalStateException e) {
Toast.makeText(this, "Error during Dropbox authentication",
Toast.LENGTH_SHORT).show();
}
}
它对我来说很好