其他分享
首页 > 其他分享> > Android Dropbox API无法保存登录信息

Android Dropbox API无法保存登录信息

作者:互联网

我是Android编程的初学者.我一直在努力让Dropbox融入我正在编写的应用程序的[开头].我按照说明查看了Dropbox API附带的基本示例DBRoulette.

我一直遇到的问题是我登录Dropbox(通过网络浏览器),然后确认该应用程序可以使用其Dropbox App文件夹……对于该会话,它工作正常,但是当我完全关闭应用程序并打开它再次,我被要求再次登录!我绝对不希望再次重新输入我的所有Dropbox登录内容,即使这仅仅是出于调试目的.
有趣的是DBRoulette工作正常,我不必每次都登录!我复制粘贴了该示例中的大部分功能代码.

我们在谈论它时,AccessTokens究竟包含/做什么?他们是否存储信息以创建授权会话?这些信息是否与我从Dropbox开发者网站获得的App key / secret组合不同?我认为这是我的错误,但我不确定.

这是活动:

package com.JS.music;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.TokenPair;
import com.dropbox.client2.session.Session.AccessType;



public class MainActivity extends Activity {

private static String TAG = "MainActivity";

private Button gotoRecordingButton;
private Button libraryButton;

//Dropbox
final static private String APP_KEY = "xxxxxxxxxxxxx";
final static private String APP_SECRET = "xxxxxxxxxxxxxx"; 
final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER; 
private DropboxAPI<AndroidAuthSession> mDBApi; 
final static public String ACCOUNT_PREFS_NAME = "MusicDBPrefs";
final static public String ACCESS_KEY_NAME = "Music_DB_ACCESS_KEY";
final static public String ACCESS_SECRET_NAME = "Music_DB_ACCESS_SECRET";

private boolean mIsLoggedIn = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gotoRecordingButton = (Button) findViewById(R.id.goto_recording_button);
    gotoRecordingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, RecordActivity.class);
            startActivity(intent);
        }
    });

    libraryButton = (Button) findViewById(R.id.library_button);
    libraryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

    AndroidAuthSession session = buildSession();
    mDBApi = new DropboxAPI<AndroidAuthSession>(session);
    mDBApi.getSession().startAuthentication(MainActivity.this);

    setLoggedIn(mDBApi.getSession().isLinked());

    Toast msg = Toast.makeText(this, "logged in: " + isLoggedIn(), Toast.LENGTH_LONG);
    msg.show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


//-------------Dropbox stuff  for testing and debugging---------

@Override
protected void onResume() {
    super.onResume();
    AndroidAuthSession session = mDBApi.getSession();

    // The next part must be inserted in the onResume() method of the
    // activity from which session.startAuthentication() was called, so
    // that Dropbox authentication completes properly.
    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) {
            Log.i(TAG, "Error authenticating", e);
        }
    }
}


//copied from dropbox API
private void storeKeys(String key, String secret) {
    // Save the access key for later
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
    Editor edit = prefs.edit();
    edit.putString(ACCESS_KEY_NAME, key);
    edit.putString(ACCESS_SECRET_NAME, secret);
    edit.commit();
}


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

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);
    }

    return session;
}

public void setLoggedIn(boolean loggedIn) {
    mIsLoggedIn = loggedIn;
}

public boolean isLoggedIn() {
    return mIsLoggedIn;
}
}

任何帮助赞赏!

解决方法:

使用DropboxApi时的主要想法是:在第一次连接时,您必须获取密钥,下次必须使用这些密钥才能访问而无需通过浏览器确认.

即在onResume方法中你应该使用这一行

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

其中DropboxAPI mDBApi;

然后你需要保存数据

AccessTokenPair tokens 

在sqlite或SharedPrefs中.

那你应该使用这样的方法:

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是我通过ORMLite存储密钥的类:

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

最后一件事:当你拿到所有钥匙时,你不应该跑

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

只需使用mDBApi作为目标,例如“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.

标签:dropbox-api,android
来源: https://codeday.me/bug/20190723/1508485.html