Dropbox使用核心APi自动验证

时间:2014-10-09 05:12:53

标签: android dropbox dropbox-api

我想避免用户立即从浏览器登录,代码如下所示。 想要避免登录屏幕mApi.getSession()。startAuthentication(this); - 这将启动登录我删除了这个和硬编码令牌密钥和令牌密钥

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        AndroidAuthSession session = buildSession();
        mApi = new DropboxAPI<AndroidAuthSession>(session);
        checkAppKeySetup();
    } catch (Exception e) {
        Logger.Error("onCreate", e);
    }
}
protected void onResume() {
    super.onResume();
    // Dropbox
    AndroidAuthSession session = mApi.getSession();
    if (session.authenticationSuccessful()) {
        try {
            // Mandatory call to complete the auth
            session.finishAuthentication();
            // Store it locally in our app for later use
            TokenPair tokens = session.getAccessTokenPair();
            storeKeys(tokens.key, tokens.secret);
            setLoggedIn(true);
        } catch (IllegalStateException e) {
            Toast.makeText(
                    this,
                    "Couldn't authenticate with Dropbox:"
                            + e.getLocalizedMessage(), Toast.LENGTH_SHORT)
                    .show();
            Logger.Error("onCreate", e);
        }
        Log.i("On resume method", "authentication was active");
    }
}

private AndroidAuthSession buildSession() {
    AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
    AndroidAuthSession session;
    String[] stored = getKeys();
    if (stored != null) {
        AccessTokenPair accessToken = new AccessTokenPair(stored[0],
                stored[1]);
        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE,
                accessToken);
    } else {
        session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
    }


private String[] getKeys() {
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    String key = prefs.getString(ACCESS_KEY_NAME, "XXXXXXXX");
    String secret = prefs.getString(ACCESS_SECRET_NAME, "xxxxxxx");
    if (key != null && secret != null) {
        String[] ret = new String[2];
        ret[0] = key;
        ret[1] = secret;
        return ret;
    } else {
        return null;
    }
}

session.authenticationSuccessful()总是返回false,我无法将文件上传到dropbox。提前致谢

1 个答案:

答案 0 :(得分:0)

我想你只想要这个:

// OAUTH_TOKEN and OAUTH_TOKEN_SECRET are the hardcoded credentials you want to use
mApi = new DropboxAPI<AndroidAuthSession>(new AndroidAuthSession(
    new AppKeyPair(APP_KEY, APP_SECRET), ACCESS_TYPE,
    new AccessTokenPair(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)));

我强烈反对这种做法 。如果您为自己的帐户硬编码OAuth令牌,那么拥有您应用的任何人都可以提取令牌,然后删除所有文件,使用您的帐户分享非法内容等等。总之,它非常糟糕想法。

Dropbox API的典型用途是让每个用户在您的应用中使用自己的帐户。