如何保存dropbox用户详细信息,使其在每次Android应用程序启动时都不会进行身份验证?
我正在构建一个android应用程序,用户必须从Dropbox下载图片。但是,每次用户都必须对自己进行身份验证。我希望应用程序第一次保存详细信息,之后不需要验证。代码如下:如何保存dropbox用户详细信息,使其在每次Android应用程序启动时都不会进行身份验证?,android,dropbox-api,Android,Dropbox Api,我正在构建一个android应用程序,用户必须从Dropbox下载图片。但是,每次用户都必须对自己进行身份验证。我希望应用程序第一次保存详细信息,之后不需要验证。代码如下: protected void initialize_session(){ AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); AndroidAuthSession session = new AndroidAuthSession(appKeys)
protected void initialize_session(){
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
mDBApi.getSession().startOAuth2Authentication(Control_Gate.this);
}
protected void onResume() {
if (mDBApi.getSession().authenticationSuccessful()) {
try {
// Required to complete auth, sets the access token on the session
mDBApi.getSession().finishAuthentication();;
String accessToken = mDBApi.getSession().getOAuth2AccessToken();
} catch (IllegalStateException e) {
Log.i("DbAuthLog", "Error authenticating", e);
}
}
super.onResume();
}
protectedvoid初始化\u会话(){
AppKeyPair appKeys=新的AppKeyPair(应用程序密钥,应用程序密钥);
AndroidAuthSession会话=新的AndroidAuthSession(appKeys);
mDBApi=新的DropboxAPI(会话);
mDBApi.getSession().startOAuth2Authentication(Control_Gate.this);
}
受保护的void onResume(){
if(mDBApi.getSession().authenticationSuccessful()){
试一试{
//需要完成身份验证,请在会话上设置访问令牌
mDBApi.getSession().finishAuthentication();;
字符串accessToken=mDBApi.getSession().getOAuth2AccessToken();
}捕获(非法状态){
Log.i(“DbAuthLog”,“错误验证”,e);
}
}
super.onResume();
}
这用于将用户返回到应用程序。我知道解决方案必须在这两个方面,但我不知道如何保存凭据。在共享首选项/SQLite中保存您的accessToken 例如
SharedPreferences sp = getSharedPreferences(
"First_share_memory", Activity.MODE_APPEND);
// save in cache memory
sp.edit().putString("accesstoken", accessToken).commit();
并在getDropboxAPI方法中使用:
private DropboxAPI <AndroidAuthSession> getDropboxAPI() {
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
SharedPreferences sharedpreferences = getSharedPreferences("First_share_memory", Activity.MODE_APPEND);
String savedAccessToken = sharedpreferences.getString("accesstoken", "");// get previously saved accessToken
if (!TextUtils.isEmpty(savedAccessToken)) {
mDBApi.getSession().setOAuth2AccessToken(savedAccessToken);
}
return mDBApi;
}
私有DropboxAPI将您的accessToken保存在共享首选项/SQLite中
例如
SharedPreferences sp = getSharedPreferences(
"First_share_memory", Activity.MODE_APPEND);
// save in cache memory
sp.edit().putString("accesstoken", accessToken).commit();
并在getDropboxAPI方法中使用:
private DropboxAPI <AndroidAuthSession> getDropboxAPI() {
AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
mDBApi = new DropboxAPI<AndroidAuthSession>(session);
SharedPreferences sharedpreferences = getSharedPreferences("First_share_memory", Activity.MODE_APPEND);
String savedAccessToken = sharedpreferences.getString("accesstoken", "");// get previously saved accessToken
if (!TextUtils.isEmpty(savedAccessToken)) {
mDBApi.getSession().setOAuth2AccessToken(savedAccessToken);
}
return mDBApi;
}
专用DropboxAPI将您的令牌保存到SharedReference,然后相应地使用它。下面是相同的示例代码。
在onResume功能中进行以下更改:
protected void onResume() {
AndroidAuthSession session = mApi.getSession();
setLoggedIn(mApi.getSession().authenticationSuccessful());
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) {
showToast(getString(R.string.could_not_authenticate_with_dropbox)
+ e.getLocalizedMessage());
}
}
super.onResume();
}
添加storeKeys和clearKeys函数以在SharedReferences中保存值
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 void clearKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
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) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
并按如下方式初始化会话:
public 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;
}
编辑:添加这三个常量,您可以对setLoggedIn的调用进行注释(true)
将您的令牌保存到SharedReference,然后相应地使用它。下面是相同的示例代码。
在onResume功能中进行以下更改:
protected void onResume() {
AndroidAuthSession session = mApi.getSession();
setLoggedIn(mApi.getSession().authenticationSuccessful());
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) {
showToast(getString(R.string.could_not_authenticate_with_dropbox)
+ e.getLocalizedMessage());
}
}
super.onResume();
}
添加storeKeys和clearKeys函数以在SharedReferences中保存值
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 void clearKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
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) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
并按如下方式初始化会话:
public 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;
}
编辑:添加这三个常量,您可以对setLoggedIn的调用进行注释(true)
帐户名称、访问密钥名称和访问密钥名称是什么?我在那里得到了错误,什么是setLoggedIn()和getKeys()?它们是由Android提供的吗?我在使用它们时也会出错帐户名、访问密钥名和访问密钥名是什么?我在那里得到了错误,什么是setLoggedIn()和getKeys()?它们是由Android提供的吗?我在使用它们的时候也会出错谢谢你这么多的努力。只需要在onResume方法中包含:else{mDBApi.getSession().startOAuth2Authentication(Control_Gate.this);}另一个问题我正在通过pubnub向android应用程序发送消息。我已经在一个服务中编写了代码,但现在安卓操作系统正在终止该服务。我也使用过START_STICKY,但它会多次重新启动服务,我也会错过一些通知。它只有512MB内存。有没有其他方法可以让手机代码不挂起?请创建一个新问题。根据SO标准,每个问题都有一个新的线程。有关详细信息,您应该阅读后台和前台服务。请参阅此处以获取参考。非常感谢您的帮助。只需要在onResume方法中包含:else{mDBApi.getSession().startOAuth2Authentication(Control_Gate.this);}另一个问题我正在通过pubnub向android应用程序发送消息。我已经在一个服务中编写了代码,但现在安卓操作系统正在终止该服务。我也使用过START_STICKY,但它会多次重新启动服务,我也会错过一些通知。它只有512MB内存。有没有其他方法可以让手机代码不挂起?请创建一个新问题。根据SO标准,每个问题都有一个新的线程。有关详细信息,请阅读后台和前台服务。请参阅此处以获取参考。