[英]How to save session of dropbox api in android
我正在使用 android 中的 dropbox api 在 dropbox 中上傳文件。 我已成功將文件上傳到保管箱中,但問題是每次我都需要允許訪問保管箱。 每次我都必須轉到瀏覽器並允許訪問。 我不知道如何解決這個問題。 我使用這些代碼在 dropbox 中上傳文件:
public class DropboxFileUploadMainForContact extends Activity {
private static final int TAKE_PHOTO = 1;
final String DIR = "/";
private File f;
private boolean mLoggedIn, onResume;
public DropboxAPI<AndroidAuthSession> mApi;
String upload_filepath = "abc/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the file which we want to upload
Bundle b = getIntent().getExtras();
upload_filepath = upload_filepath + b.getString("fileNameToUpload");
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
setLoggedIn(false);
createDir();
if (mLoggedIn) {
logOut();
}
if (Utils.isOnline(DropboxFileUploadMainForContact.this)) {
mApi.getSession().startAuthentication(DropboxFileUploadMainForContact.this);
onResume = true;
} else {
Utils.showNetworkAlert(DropboxFileUploadMainForContact.this);
finish();
}
}
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;
}
}
private void logOut() {
mApi.getSession().unlink();
clearKeys();
}
private void clearKeys() {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.clear();
edit.commit();
}
private void createDir() {
File dir = new File(Utils.getPath());
if (!dir.exists()) {
dir.mkdirs();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PHOTO) {
// f = new File(Utils.getPath() + "/temp.jpg");
if (Utils.isOnline(DropboxFileUploadMainForContact.this)) {
mApi.getSession().startAuthentication(
DropboxFileUploadMainForContact.this);
onResume = true;
} else {
Utils.showNetworkAlert(DropboxFileUploadMainForContact.this);
}
}
}
}
public void setLoggedIn(boolean loggedIn) {
mLoggedIn = loggedIn;
if (loggedIn) {
// new
// Now make a backup file of sms
// BackUpAllSms();
DropboxFileUpload uploadFile = new DropboxFileUpload(this, mApi,
DIR, upload_filepath);
uploadFile.execute();
onResume = false;
finish();
}
}
private void storeKeys(String key, String secret) {
SharedPreferences prefs = getSharedPreferences(
Constants.ACCOUNT_PREFS_NAME, 0);
Editor edit = prefs.edit();
edit.putString(Constants.ACCESS_KEY_NAME, key);
edit.putString(Constants.ACCESS_SECRET_NAME, secret);
edit.commit();
}
private void showToast(String msg) {
Toast error = Toast.makeText(this, msg, Toast.LENGTH_LONG);
error.show();
}
@Override
protected void onResume() {
try {
AndroidAuthSession session = mApi.getSession();
Log.d("ppp", "session.authenticationSuccessful() on resume = "+session.authenticationSuccessful());
if (session.authenticationSuccessful()) {
try {
session.finishAuthentication();
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(onResume);
} catch (IllegalStateException e) {
showToast("Couldn't authenticate with Dropbox:"
+ e.getLocalizedMessage());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.onResume();
}
}
誰能告訴我如何解決這個問題??
為了避免每次都手動授權應用程序連接到用戶的 Dropbox 帳戶(即調用startAuthentication
),應用程序應存儲並重新使用生成的訪問令牌,這樣只需執行一次。
似乎您已經在buildSession
中使用了storeKeys
和getKeys
來執行此操作,它們使用SharedPreferences
來保留訪問令牌。
因此,您只需要知道AndroidAuthSession
已經加載了訪問令牌。 您可以為此使用AndroidAuthSession.isLinked
方法:
在調用startAuthentication
之前調用startAuthentication
這樣您就可以知道您是否已經擁有訪問令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.