Android Dropbox API not saving login

陌路散爱 提交于 2019-12-06 02:57:57
Aleksandr

The main idea when you use DropboxApi is: in the 1st time when you connected you must get secret keys, next time you must use these keys for access without confirming via browser.

i.e. in onResume method you should use this line

AccessTokenPair tokens = mDBApi.getSession().getAccessTokenPair();

where DropboxAPI mDBApi;

Then you need to save data of

AccessTokenPair tokens 

in sqlite or SharedPrefs.

Then you should use method like this:

private DropboxAPI <AndroidAuthSession> getDropboxAPI(){
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    AccessKeys keys = dm.getAccessKeys(APP_KEY); //dm is DatabaseManager of ORMLite
    if (keys == null) return null;
    AccessTokenPair access = new AccessTokenPair(keys.getAccessKey(), keys.getAccessSecret());
    mDBApi.getSession().setAccessTokenPair(access);
    return mDBApi;
}

...

AccessKeys is class where I stored keys via ORMLite:

@DatabaseTable
public class AccessKeys {
... 
    @DatabaseField private String accessKey;
    @DatabaseField private String accessSecret;
    @DatabaseField private String appKey;
    @DatabaseField private String appSecret;
...}

And last one thing: when you got all keys you should not run

mDBApi.getSession().startAuthentication(MainActivity.this);

just use mDBApi for your goals, like "mDBApi.putFile(some data)"

Note: You only need to do startAuthentication() once, You don't need to run it to authorize the key pair from the actual login.

Vesko

I know it's an old question, but answering it just in case someone else stumbles upon it.

As for the main described problem - that you have to login into Dropbox again each time, that's simply because you have this line near the end of onCreate()

mDBApi.getSession().startAuthentication(MainActivity.this);

startAuthentication() always starts a new "login" flow, no matter if you already have a valid session or not. For this reason you should NOT call it every time.

The accepted answer is good, but you can get away with using less code, by saving the accessToken. So first, in onResume() after finishAuthentication() you'd save the accessToken like this

String accessToken = mDBApi.getSession().getOAuth2AccessToken();
// save accessToken to SQLite or SharedPrefs or whatever

Then the getDropboxAPI() method suggested by @Alexandr should look like this

private DropboxAPI <AndroidAuthSession> getDropboxAPI() {
    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);

    String savedAccessToken = // get previously saved accessToken

    if (!TextUtils.isEmpty(savedAccessToken)) {
        mDBApi.getSession().setOAuth2AccessToken(savedAccessToken);
    }

    return mDBApi;
}

It's also a good idea to create a helper method like this to check if Dropbox is properly initialised before using it for real work, like uploading or downloading files.

public boolean isDropboxLinked() {
    return mDBApi != null && (mDBApi.getSession().isLinked() || mDBApi.getSession().authenticationSuccessful());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!