2012-11-20 33 views
9

我需要多個帳戶如何在Android Dropbox sdk 1.5.1中驗證多個帳戶?

我已經搜索論壇

認證,而且好像有可能 所以我給它一個嘗試,但我失敗了

使用相同的API APP_KEY & APP_SECRET我試過,它未能 我的兩個會議返回相同的訪問令牌對

所以我嘗試使用不同的API APP_KEY & APP_SECRET,在相同的Dropbox帳戶,它沒有太多

所以我再次嘗試使用不同的API APP_KEY &來自不同Dropbox帳戶的APP_SECRET,它仍然失敗

任何人都可以爲我提供解決方案嗎?在此先感謝

下面是我的代碼,主要來自DBroulette例如

的onCreate(機器人)

AndroidAuthSession session = buildSession(); 
mApi = new DropboxAPI<AndroidAuthSession>(session); 
AndroidAuthSession session2 = buildSession2(); 
mApi2 = new DropboxAPI<AndroidAuthSession>(session2); 

的onResume(機器人)

AndroidAuthSession session = mApi.getSession(); 
if (session.isLinked()) { 
dbsetLoggedIn(true); 
} else { 
dbsetLoggedIn(false); 
} 
if (session.authenticationSuccessful()) { 
try { 
session.finishAuthentication(); 
TokenPair tokens = session.getAccessTokenPair(); 
dbstoreKeys(tokens.key, tokens.secret); 
dbsetLoggedIn(true); 
statusTv.append("Dropbox authentication successful\n"); 
} catch (IllegalStateException e) { 
Log.i("Dropbox Error", "Error authenticating", e); 
} 
} 

AndroidAuthSession session2 = mApi2.getSession(); 
if (session2.isLinked()) { 
dbsetLoggedIn2(true); 
} else { 
dbsetLoggedIn2(false); 
} 
if (session2.authenticationSuccessful()) { 
try { 
session2.finishAuthentication(); 
TokenPair tokens = session2.getAccessTokenPair(); 
dbstoreKeys2(tokens.key, tokens.secret); 
dbsetLoggedIn2(true); 
statusTv.append("2Dropbox authentication successful\n"); 
} catch (IllegalStateException e) { 
Log.i("Dropbox Error", "Error authenticating", e); 
} 
} 

OTHERS碼

private AndroidAuthSession buildSession() { 
AppKeyPair appKeyPair = new AppKeyPair(Constants.APP_KEY, Constants.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 buildSession2() { 
AppKeyPair appKeyPair = new AppKeyPair(Constants.APP_KEY2, Constants.APP_SECRET2); 
AndroidAuthSession session; 
String[] stored = getKeys2(); 
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 String[] getKeys2() { 
SharedPreferences prefs = getSharedPreferences(Constants.ACCOUNT_PREFS_NAME, 0); 
String key = prefs.getString(Constants.ACCESS_KEY_NAME2, null); 
String secret = prefs.getString(Constants.ACCESS_SECRET_NAME2, null); 
if (key != null && secret != null) { 
String[] ret = new String[2]; 
ret[0] = key; 
ret[1] = secret; 
return ret; 
} else { 
return null; 
} 
} 

我注意到我可能需要添加一些東西到清單中,加入另一個 但是我不能在Android清單中添加不同的APP KEY中的第二個活動,因爲它會導致重複的錯誤 我該怎麼做?

<activity 
android:name="com.dropbox.client2.android.AuthActivity" 
android:configChanges="orientation|keyboard" 
android:launchMode="singleTask" > 
<intent-filter> 
<data android:scheme="db-XXXXXXXXXXXX" /> 

<action android:name="android.intent.action.VIEW" /> 

<category android:name="android.intent.category.BROWSABLE" /> 
<category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
</activity> 
+0

仍在尋求幫助 – tcboy88

+0

你做了這項工作嗎? – tipycalFlow

回答

1

我遇到了類似的需求,這就是我的工作方式。

1st App
使用正常的保管箱流訪問您的第一個應用程序。

注意: 對於2個dropbox應用程序要求的可能情況可能是使用不同的Dropbox應用程序從您的服務器訪問用戶帳戶。請注意,如果您在服務器上使用相同的Dropbox應用程序,則可以與第一個應用程序共享您的服務器的訪問令牌並安全地重用這些憑據。如果你不能忍受,繼續閱讀。

第二應用
選項1:使用其他Android應用

  1. 只是爲第二Dropbox的應用程序的OAuth的流程創建另一個Android應用程序。
  2. 使用Intent從app1觸發app2中的oAuthflow。
  3. 同樣,使用意圖從APP 2發回的令牌數據APP1

的一些技巧,如果你要使用這樣的:

  1. 讓應用2 OAuth的活動透明
  2. 的背景
  3. 對APP <刪除意圖改變動畫 - > APP 2過渡
  4. 觸發的OAuth的應用2活動的onCreate

選項2:如果您一直只使用一個Android應用程序執行此操作,我發現了一種可能的解決方法,如下所述。

  1. 提示用戶打開這個網址: https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=APP2_CLIENT_ID
  2. 他們將不得不復制回來的Dropbox
  3. 該授權碼返回授權碼可用於獲取access_tokens的第二應用

如果您打算在服務器端上下文中使用第二個應用程序,只需與服務器共享授權碼。您可以從授權代碼標記,在python的流程,就像這樣:

flow = client.DropboxOAuth2FlowNoRedirect(app2_key, app2_secret) 
authorize_url = flow.start() 
access_token, user_id = flow.finish(auth_code_from_client) 

對於更通用的方式來獲得授權密鑰access_tokens,look at this

0

的Dropbox API是有一些問題,或者你可以說一你需要使用的技巧才能進行多次登錄。

1.聲明sAuthenticatedUid作爲字符串[]

private static final String[] sAuthenticatedUid = { "dummy"}; // Keeping only one Auth Id to keep last authenticated item

2.啓動的OAuth使用不同的方法

使用session.startOAuth2Authentication(act, "", sAuthenticatedUid)進行認證,而不是startOAuth2Authentication()

3.保持變量的正確性ntication成功

sAuthenticatedUid[0] = sessionApi.getSession().finishAuthentication(); // Save the last successful UID 
String oauth2AccessToken = sessionApi.getSession().getOAuth2AccessToken(); 
AuthActivity.result = null; // Reset this so that we can login again, call only after finishAuthentication() 

AuthActivity是com.dropbox.client2.android.AuthActivity存儲從最後驗證的結果,因爲這是靜態變量可能產生問題。

您應該可以根據需要進行多次登錄。