问题
I am implementing dropbox in my app.When I am clicking a button I am connecting to dropbox.
The problem
Each and every time when I click the button,the dropbox confirmation dialog pops up and I want to avoid that after successful auithentication and one time confirmation.
The code
public static boolean mLoggedIn = false;
private boolean isItemClicked = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dropboxdownload);
lvDropboxDownloadFilesList = (ListView) findViewById(R.id.lvDropboxDownloadFilesList);
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
if (!Constants.mLoggedIn)
mApi.getSession().startAuthentication(DropboxDownload.this);
lvDropboxDownloadFilesList.setOnItemClickListener(this);
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(Constants.DROPBOX_APP_KEY,
Constants.DROPBOX_APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0],
stored[1]);
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE,
accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
}
return session;
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(Constants.DROPBOX_APP_KEY,
Constants.DROPBOX_APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0],
stored[1]);
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE,
accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, Constants.ACCESS_TYPE);
}
return session;
}
private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(Constants.ACCESS_KEY_NAME, null);
String secret = prefs.getString(Constants.ACCESS_SECRET_NAME, null);
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
@Override
protected void onResume() {
super.onResume();
AndroidAuthSession session = mApi.getSession();
if (session.authenticationSuccessful()) {
try {
session.finishAuthentication();
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(true);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:"
+ e.getLocalizedMessage());
}
}
}
public void setLoggedIn(final boolean loggedIn) {
pd = ProgressDialog.show(DropboxDownload.this, null,
"Retrieving data...");
new Thread(new Runnable() {
@Override
public void run() {
Constants.mLoggedIn = loggedIn;
if (loggedIn) {
int i = 0;
com.dropbox.client2.DropboxAPI.Entry dirent;
try {
dirent = mApi.metadata(DIR, 1000, null, true, null);
files = new ArrayList<com.dropbox.client2.DropboxAPI.Entry>();
dir = new ArrayList<String>();
for (com.dropbox.client2.DropboxAPI.Entry ent : dirent.contents) {
files.add(ent);
dir.add(new String(files.get(i++).path));
}
i = 0;
mHandler.sendEmptyMessage(0);
} catch (DropboxException e) {
e.printStackTrace();
}
}
}
}).start();
I consulted stack overflow and find that I have to check the access secret and access key each time that it is null or not and I have checked that(see buildSession()).But still the problem is persisting.What wrong I am doing??Help please.I am using core api.
回答1:
How to set access user access token pair manually.
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
if (mDBApi == null) {
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
// mDBApi.getSession().startAuthentication(Main.this); //kicks off the web-based authentication
//you'll have to use the web-based authentication UI one-time to get the ######### values
String token_key="#########";
String token_seceret="#########";
AccessTokenPair tokens=new AccessTokenPair(token_key,token_seceret);
mDBApi.getSession().setAccessTokenPair(tokens);
// boolean v=mDBApi.getSession().authenticationSuccessful();
}
First time i run application in debug mode with break point i get the token key and token secret of by entering valid log in detail.and saved(noted) that credential and after that i set them manually as above code then can be log in successfully.
And also have a look at Dropbox example app DBRoulette.
info from here
来源:https://stackoverflow.com/questions/27046540/dropbox-confirmation-dialog-pop-up-every-times-when-button-is-clicked