每次单击按钮时,都会弹出Dropbox确认对话框

时间:2014-11-20 18:08:09

标签: android dropbox-api

我正在我的应用中实现dropbox。当我点击我连接到Dropbox的按钮时。

问题

每次单击按钮时,都会弹出Dropbox确认对话框,我希望在成功进行身份验证和一次确认后避免这种情况。

代码

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

我查询了堆栈溢出,并发现每次它是否为null时我都必须检查访问密钥和访问密钥,并且我已经检查过(参见buildSession())。但问题仍然存在。我的错我在做什么?请帮忙。我正在使用核心API。

1 个答案:

答案 0 :(得分:0)

如何手动设置访问用户访问令牌对。

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

我第一次在调试模式下使用断点运行应用程序我通过输入有效的详细记录来获取令牌密钥和令牌秘密。并保存(注明)该凭证,然后我按照上面的代码手动设置它们然后可以登录成功。

还可以看一下Dropbox示例应用程序DBRoulette。

来自here

信息