Android Dropbox API无法保存登录 [英] Android Dropbox API not saving login

查看:58
本文介绍了Android Dropbox API无法保存登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的初学者。我一直在尝试让Dropbox集成到我正在编写的应用程序的[开始]中。我已按照说明进行操作,并查看了Dropbox API附带的基本示例DBRoulette。

I'm sort of a beginner at Android programming. I've been trying to get Dropbox to integrate into [the beginnings of] an app I'm writing. I've followed the instructions and looked at the basic example DBRoulette that comes with the Dropbox API.

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

The problem I keep running into is that I log into Dropbox (through web browser) and then confirm that the app can use its Dropbox App folder... For that session it works fine, but then when I close the app fully and open it again, I get asked to log in again! I definitely don't want to have to retype all my dropbox login stuff again even if this is just for debugging purposes. The interesting thing is that DBRoulette works fine, I don't have to log in each time! And I copy pasted much of the functional code from that example.

在使用该代码时,AccessToken到底包含/执行了什么操作?他们是否存储信息以创建授权的会话?此信息是否与我从Dropbox开发人员网站上获得的应用程序密钥/秘密组合不同?我认为这是我的错误所在,但我不确定。

While we're at it, what exactly do the AccessTokens contain/do? Do they store the information to create an authorized session? Is this information different from the App key/secret combo I get from the Dropbox developer site? I think this is where my error is but I am not sure.

这里是活动:

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的主要思想是:在第一次连接时,您必须获得秘密密钥,下次您必须使用这些密钥进行访问而无需通过浏览器进行确认。

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.

ie在onResume方法中,您应该使用此行

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

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

其中DropboxAPI mDBApi;

where DropboxAPI mDBApi;

然后您需要保存

AccessTokenPair tokens 

在sqlite或SharedPrefs中。

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

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

只需将mDBApi用于您的目标,例如 mDBApi.putFile(某些数据)

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


注意:您只需要执行一次startAuthentication(),而无需运行它来授权密钥对

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

这篇关于Android Dropbox API无法保存登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆